1

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").

Honeybear
  • 2,928
  • 2
  • 28
  • 47
  • Probably trying to differentiate between files and directories – Mat Apr 05 '23 at 07:18
  • 3
    There is no advantage in using `\nul`, but for me, the safest option is `\.` Your command will look like this: ```If Not Exist "%~1\." GoTo errorDir```. – Compo Apr 05 '23 at 08:29

0 Answers0