Checking if a folder exists was not as simple as it seemed in "real" DOS (COMMAND.COM):
IF EXIST d:\somefolder ECHO d:\somefolder exists
does not work in COMMAND.COM!
In COMMAND.COM we had to check for the existence of a device (e.g.
NUL) in a folder to make sure that folder existed:
IF EXIST d:\somefolder\NUL ECHO Folder d:\somefolder exists
works like a charm — in COMMAND.COM.
However, these devices do not always exist in every folder in Windows NT 4 and later; Aaron Thoma found out the hard way that this will not work if the path contains junctions or symbolic links.
So in NT, the old way of checking for the existence of folders by checking for a device name should no longer be used!
Windows NT 4 and later (CMD.EXE) introduced simpler ways to check if a folder exists:
IF EXIST d:\somefolder\ ECHO Folder d:\somefolder exists
will work as expected in NT (but not in COMMAND.COM).
Note the trailing backslash, which makes sure you won't get a false positive if a file named
An alternative method, rather more complex, uses
PUSHD which can only push folders onto the stack, not files:
PUSHD d:\somefolder && POPD || ECHO d:\somefolder does NOT exist
Sometimes (not often, these days, I presume) a batch file may have to work in all DOS and Windows versions; in that case, the following trick can be used to check for the existence of a folder:
SET NUL=NUL IF "%OS%"=="Windows_NT" SET NUL= • • • IF EXIST d:\somefolder\%NUL% ECHO Folder d:\somefolder exists
|page last uploaded: 2018-04-18, 10:12|