238

I would like to be able to check if a certain folder (FolderA) exists and if so, for a message to be displayed and then the batch file to be exited.

If FolderA does not exist, I would then like to check if another folder (FolderB) exists. If FolderB does not exist, a message should be displayed and the folder should be created, and if FolderB does exist, a message should be displayed saying so.

Does anybody have any idea on the code I could simply use on notepad to create a batch file to allow me to do this?

All of this needs to be done in one .bat file.

Jost
  • 5,948
  • 8
  • 42
  • 72
user3179825
  • 2,443
  • 2
  • 12
  • 4
  • 7
    A google search comes up with [Testing if a Drive or Directory Exists](http://support.microsoft.com/kb/65994). And this explains exactly how to do what you are asking for... – NealB Jan 10 '14 at 00:11
  • 2
    A search for "batch-file directory exists" here on SO found [Windows Batch File Look for directory if not exist create then move file](http://stackoverflow.com/questions/17899191), which shows you how to see if a directory exists or not. There are posts here about creating a directory from a batch file as well. Please do some basic research before posting new questions; chances are good the question has been asked here previously. Thanks. – Ken White Jan 10 '14 at 00:18
  • 4
    Windows batch is a programming language, check if the file exists is a programming task. Appears on topic. – Audrius Meškauskas Oct 01 '15 at 10:56
  • 3
    Should be closed as dupicate of [How to test if a file is a directory in a batch script?](//stackoverflow.com/q/138981) instead of "off-topic" – Michael Freidgeim Sep 28 '17 at 12:59
  • [@NealB](https://stackoverflow.com/users/192510/nealb), the link to that Microsoft knowledge base article returns a 404. However, you can view [this archived version of the article](https://jeffpar.github.io/kbarchive/kb/065/Q65994). – homersimpson Apr 21 '22 at 15:18
  • to whomever closed this with "insufficient information" FU you are the problem with SO – Mehdi Aug 31 '23 at 14:33

2 Answers2

381

For a file:

if exist yourfilename (
  echo Yes 
) else (
  echo No
)

Replace yourfilename with the name of your file.

For a directory:

if exist yourfoldername\ (
  echo Yes 
) else (
  echo No
)

Replace yourfoldername with the name of your folder.

A trailing backslash (\) seems to be enough to distinguish between directories and ordinary files.

official documentation for if

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
09stephenb
  • 9,358
  • 15
  • 53
  • 91
  • 2
    The second part of your answer is incomplete. – tmj Nov 30 '14 at 17:52
  • 5
    `if exist "c:\folder\nul" echo ok` did not work for me under Windows 7. What worked was `if exist "c:\folder\" echo ok`. – Alexander Gelbukh Mar 29 '15 at 22:26
  • 3
    @AlexanderGelbukh - You are correct, the `if exist "networkPath\nul"` can falsely report a file as a folder on Windows network drives. Unfortunately, the `if exist "networkPath\"` suffers the same problem. See the 2015-12-08 update at http://stackoverflow.com/a/8669636/1012053 for a technique that has been reliable in all my testing. – dbenham Dec 08 '15 at 17:11
  • @AlexanderGelbukh It works without the quotes. Of course, this limits you when it comes to having spaces in the directory path. – Marc.2377 Dec 12 '16 at 03:34
  • 2
    Yes, without quotes it works. However, with quotes it does not work even for local drives: `if exist c:\Windows\nul echo ok` says `ok`, but `if exist "c:\Windows\nul" echo ok` says nothing. As I have said, while `if exist "c:\Program Files\nul" echo ok` says nothing, `if exist "c:\Program Files\" echo ok` says `ok` and thus works even for path with spaces. – Alexander Gelbukh Dec 12 '16 at 21:57
  • 1
    Microsoft link is broken. You've better remove it. – mchar May 29 '18 at 09:32
  • Missing '(' after 'if' in if statement. – Jonathan Sep 15 '22 at 18:05
  • As far as I recall, `if exist "SomePath\*"` should even work for network paths… – aschipfl Nov 08 '22 at 21:56
  • IF exist newdir (echo newdir Directory exists ) ELSE (mkdir newdir && echo newdir is now created ) >>> This has worked for me. – Sujit Neb Apr 17 '23 at 12:14
41

I think the answer is here (possibly duplicate):

How to test if a file is a directory in a batch script?

IF EXIST %VAR%\NUL ECHO It's a directory

Replace %VAR% with your directory. Please read the original answer because includes details about handling white spaces in the folder name.

As foxidrive said, this might not be reliable on NT class windows. It works for me, but I know it has some limitations (which you can find in the referenced question)

if exist "c:\folder\" echo folder exists 

should be enough for modern windows.

Community
  • 1
  • 1
Cosmin Vană
  • 1,562
  • 12
  • 28
  • 6
    The \nul technique is not reliable on NT class windows. It was fine in MSDOS and Win9x however. The solution in modern Windows is simply `if exist "c\:folder\" echo folder exists` – foxidrive Jan 10 '14 at 04:30
  • 1
    I think this is also specified in the referenced link. – Cosmin Vană Jan 10 '14 at 11:17
  • Still don't get it why I got downvoted (which means "Not useful"). I added the reference to the question where the answers cover the required scenario. – Cosmin Vană Jan 10 '14 at 11:27
  • Not useful could be because it is not applicable to current machines, and your earlier answer didn't make any distinction. Some people will still downvote your answer because it doesn't answer the question as posed. – foxidrive Jan 10 '14 at 12:05
  • 2
    from http://support.microsoft.com/kb/65994 "NUL always exists on a local MS-DOS FAT drive" - as in `E:\NUL` – CAD bloke Jan 12 '15 at 11:11
  • `if exist "SomePath\"` will also report a file to exist. As far as I recall, `if exist "SomePath\*"` should even work for network paths… – aschipfl Nov 08 '22 at 21:58