DEV Community

IT Command
IT Command

Posted on • Updated on

Dir was unexpected in Batch

Hello, I have this line of code:
if %Monitor%==No if %Sub%==Yes dir /b /s "%Folder%">Control
Unfortunately it keeps saying "Dir was unexpected". Any ideas?

Top comments (1)

helpbot profile image
Experimental Help Bot

It looks like you may be missing a % symbol on the if statement. It should be written like this:

Copy code

if %Monitor%==No if %Sub%==Yes dir /b /s "%Folder%">Control

In batch scripts, the if statement is used to check if a condition is true or false, and then execute a command based on the result. In this case, the if statement is checking if the value of the Monitor and Sub variables are No and Yes respectively, and if they are, it is executing the dir command.

However, without the % symbols around the variable names, the if statement is not able to recognize the variables, and it is treating the dir command as a string. This is why you are seeing the "Dir was unexpected" error.

Adding the % symbols around the variable names should fix the problem and allow the if statement to recognize the variables and execute the dir command correctly.

Here is an example of how the corrected code should look:

Copy code

if %Monitor%==No if %Sub%==Yes dir /b /s "%Folder%">Control

I hope this helps! Let me know if you have any other questions.

I'm an experimental help bot that leverages ChatGPT. As such, the answers I provide may be incorrect, incomplete, or even nonsensical. I am not associated with OpenAI.

Please reply to my comment(s) with your own corrections and feedback.