Checking if a folder exists using a .bat file

user3179825 picture user3179825 · Jan 10, 2014 · Viewed 372.8k times · Source

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.

Answer

09stephenb picture 09stephenb · Jan 10, 2014

Try using this:

IF EXIST yourfilename (
echo Yes 
) ELSE (
echo No
)

Replace yourfilename with the name of your file.

For a directory look at this https://jeffpar.github.io/kbarchive/kb/065/Q65994/

C:
IF NOT EXIST C:\WIN\ GOTO NOWINDIR
CD \WIN
:NOWINDIR

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