IF EXIST
checks only if a file exists and cannot check folders. Usually, you test like this
IF NOT EXIST "myfolder\NUL" mkdir "myfolder"
The pseudo device NUL
acts like a file and does in fact exist in every folder. Note the spelling.
But I have seen that test fail in batchfiles, for unknown reasons. So I suggest this instead:
CD myfolder 2>NUL && CD .. || MD myfolder
CD myfolder
tries a legal operation with the folder and the conditional execution of MD
/MKDIR
creates the folder only if that operation fails. CD ..
reverts the action if the folder should exist. 2>NUL
suppresses the error message if the folder does not exist.
edit:
Apparantly there is a simpler method for testing: append a backslash (\) to the foldername to make it syntactically a folder, like this:
if not exist myfolder\ md myfolder || goto :EOF
This will create the folder "myfolder" if it does not yet exist. Additionally, in case there is a file named "myfolder" the MD
will fail and the batch file will be terminated after the error message is displayed. Also, ERRORLEVEL will be set. I like this more as the error output doesn't have to be redirected.
edit:
If you want to execute several commands, run them in a subshell, that is, enclose them in parantheses like this:
if not exist myfolder\ (
md myfolder
dir myfolder
REM ...or run any other commands
) || goto :EOF