Check if a file or folder exists

It can be useful in batch files to know whether a file or folder exists. One can use files as semaphores and for inter proces communication (IPC).

To check if a file exists:

IF EXIST c:\windows\ ECHO File exists!

To check if a directory exists (Notice the nul):

IF EXIST c:\windows\nul ECHO Directory exists!

Note if working with long directory names with spaces, then one needs to put quotes around the directory-/file-path (Notice that nul should not be included):

IF EXIST "C:\Program Files\" ECHO Directory exists!

More Info MS KB65994


  1. Frank says:

    This "\NUL" folder syntax is not working for me on XPsp2.

  2. Frank says:

    OK, further searching seems to indicate the “\NUL” folder syntax only works on FAT drives so it can't be depended upon.

  3. Snakefoot says:

    Frank wrote:

    The “\NUL” folder syntax only works on FAT drives so it can’t be depended upon

    I have just tested that one could use the NUL syntax in WinXP on a NTFS drive (and it works).

    The problem with the NUL syntax is that one cannot use it with long directory names. I have now updated this very old DOS tip :)

  4. AO says:

    Works on NTFS. My batch file is as follow:

    @echo off
    IF EXIST "%userprofile%\Local Settings\Application Data\Microsoft\Office\" ECHO Directory exists!

    Thanks for the post & tips, Snakefoot

  5. Mammarduk says:

    Really usefull. Good job.

  6. John says:

    Thanks for posting this info , very helpfull

  7. matthew warburton says:

    Very useful, thanks for sharing

Leave a Reply

Your email address will not be published. Required fields are marked *