wmaynard
wmaynard

Reputation: 238

Batch - IF EXIST statement not executing

I'm creating a batch file to automate some backups. I've been out of the DOS / batch game for several years and I'm pretty rusty. I'm sure my problem is pretty minor, but I can't seem to get my if statement to do anything. Below is the offending line:

IF EXIST %dirname% (echo Directory already exists) ELSE (mkdir %dirname%)
::dirname in this particular case is: Backup 2013-06-06
::The directory does exist, so the echo statement should execute.

My echo statement, even though the condition is met, isn't triggering. This line works fine when typed into the command prompt, but not from the .bat. Anyone know what the issue is?

Thanks!

Upvotes: 0

Views: 163

Answers (1)

Magoo
Magoo

Reputation: 80033

You need to "quote the name" bcause it contains a space

IF EXIST "%dirname%" (echo Directory already exists) ELSE (mkdir "%dirname%")

although

mkdir "%dirname%" 2>nul

would also work - it simply suppresses the error message output on STDERR (device 2)

Upvotes: 2

Related Questions