1f4b Check if a file or folder exists | Tips | Smallvoid.com

Check if a file or folder exists

1 January 2000 by Snakefoot | Comment » | Trackback Off
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\win.com 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 KB Q65994
Category:

Updated: 23 July 2009

Comments:

Comment by Frank - 23 July 2009 @ 19:13 Reply

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

Comment by Frank - 23 July 2009 @ 19:16 Reply

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

Comment by Snakefoot - 23 July 2009 @ 21:37 Reply

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 :)

Comment by AO - 14 September 2009 @ 14:46 Reply

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

Comment by Mammarduk - 15 November 2010 @ 21:53 Reply

Really usefull. Good job.
Thanks.

Comment by John - 12 January 2012 @ 20:48 Reply

Thanks for posting this info , very helpfull

Comment by matthew warburton - 9 August 2012 @ 10:14 Reply

Very useful, thanks for sharing

Leave a comment


NB! Use the Forum for computer help and off-topic questions.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


0