Check whether a file/folder exists, with cmd command-line (NOT batch script)

The solution when the resource is a file it is pretty straight-forward as indicated by others:

C:\> IF EXIST C:\CONFIG.SYS ECHO C:\CONFIG.SYS exists.

Unfortunately, the above does not work for directories. The EXIST function returns the same result for both missing and present folders. Fortunately, there is an obscure workaround:

C:\> IF NOT EXIST C:\FOLDER\NUL ECHO C:\FOLDER missing.
C:\FOLDER missing.
C:\> MD C:\FOLDER
C:\> IF EXIST C:\FOLDER\NUL ECHO C:\FOLDER exists.
C:\FOLDER exists.

It turns out that to support constructs like appending >NUL on command statements, there is a sort of virtual file named "NUL" in every directory. Checking for its existence is equivalent to a check for the directory's existence.

This behavior is documented in a Microsoft knowledge base article ( https://support.microsoft.com/en-us/kb/65994 ) and I have confirmed its behavior on FreeDOS 1.1 and in a Windows 7 command shell.

EXTRA: The KB article indicates this technique can also be used to see if a drive is present. In the case of checking for drive existence, however, caveats exist:

  • An Abort, Retry, Fail? error occurs if the drive is not formatted.

  • Using this technique to check for drive existence depends on device driver implementation and may not always work.


You can use a simple

DIR C:\User

You can use type command, it will return the contents of a text file without opening it, and for a directory it will return: Access is denied.

If the file or directory is not available you get the message: The system cannot find the file specified.

So for example:

C:\>type c:\temp
Access is denied.

C:\>type c:\example.txt
Some example content in a text file

C:\>type c:\doesnotexist
The system cannot find the file specified.