I found a batch script example that checks whether a folder (given as argument) exists with the following fragment:
if not exist %1\NUL goto errorDir
rem throw error if folder doesn't exist.
:errorDir
echo ERROR Input directory doesn't exist... exiting.
goto :end
This works fine when called without quotes, e.g. script.bat my\path\to\folder
. But it fails if I hand over a folder path within quotes, script.bat "my\path\to\folder with spaces"
- even though the folder exists.
I then realized that apparently I do not need the \NUL
part (from this answer here) and it will work fine with quotes:
if not exist %1 goto errorDir
Now I am wondering: Is there an advantage for using \NUL
? And how would I use it corretly so it works with quotes? Just wrapping it in quotes didn't work for me (if not exist "%1\NUL"
).