253

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.

4
  • 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, 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. Commented Jan 10, 2014 at 0:18
  • 4
    Windows batch is a programming language, check if the file exists is a programming task. Appears on topic. Commented Oct 1, 2015 at 10:56
  • 3
    Should be closed as dupicate of How to test if a file is a directory in a batch script? instead of "off-topic" Commented Sep 28, 2017 at 12:59
  • @NealB, the link to that Microsoft knowledge base article returns a 404. However, you can view this archived version of the article. Commented Apr 21, 2022 at 15:18

2 Answers 2

416

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

Sign up to request clarification or add additional context in comments.

3 Comments

Missing '(' after 'if' in if statement.
As far as I recall, if exist "SomePath\*" should even work for network paths…
IF exist newdir (echo newdir Directory exists ) ELSE (mkdir newdir && echo newdir is now created ) >>> This has worked for me.
46

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.

6 Comments

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
I think this is also specified in the referenced link.
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.
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.
from support.microsoft.com/kb/65994 "NUL always exists on a local MS-DOS FAT drive" - as in E:\NUL
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.