505
votes

Can anybody tell me how to do the following in in a Windows batch script? (*.bat):

  • Create a folder only if it doesn't already exist

In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn't already exist. I don't want to overwrite the contents of the folder if it already exists and the batch is executed.

11
How About This ? if not exist "%Folder%" mkdir "%New-Folder%"user9556248

11 Answers

678
votes

You just use this: if not exist "C:\VTS\" mkdir C:\VTS it wll create a directory only if the folder does not exist.

Note that this existence test will return true only if VTS exists and is a directory. If it is not there, or is there as a file, the mkdir command will run, and should cause an error. You might want to check for whether VTS exists as a file as well.

238
votes
if exist C:\VTS\NUL echo "Folder already exists"

if not exist C:\VTS\NUL echo "Folder does not exist"

See also https://support.microsoft.com/en-us/kb/65994

(Update March 7, 2018; Microsoft article is down, archive on https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )

62
votes

Just call mkdir C:\VTS no matter what. It will simply report that the subdirectory already exists.

Edit: As others have noted, this does set the %ERRORLEVEL% if the folder already exists. If your batch (or any processes calling it) doesn't care about the error level, this method works nicely. Since the question made no mention of avoiding the error level, this answer is perfectly valid. It fulfills the needs of creating the folder if it doesn't exist, and it doesn't overwrite the contents of an existing folder. Otherwise follow Martin Schapendonk's answer.

40
votes
mkdir C:\VTS 2> NUL

create a folder called VTS and output A subdirectory or file TEST already exists to NUL.

or

(C:&(mkdir "C:\VTS" 2> NUL))&

change the drive letter to C:, mkdir, output error to NUL and run the next command.

32
votes
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
23
votes

I use this way, you should put a backslash at the end of the directory name to avoid that place exists in a file without extension with the same name as the directory you specified, never use "C:\VTS" because it can a file exists with the name "VTS" saved in "C:" partition, the correct way is to use "C:\VTS\", check out the backslash after the VTS, so is the right way.

@echo off
@break off
@title Create folder with batch but only if it doesn't already exist - D3F4ULT
@color 0a
@cls

setlocal EnableDelayedExpansion

if not exist "C:\VTS\" (
  mkdir "C:\VTS\"
  if "!errorlevel!" EQU "0" (
    echo Folder created successfully
  ) else (
    echo Error while creating folder
  )
) else (
  echo Folder already exists
)

pause
exit
19
votes

You can use:

if not exist "C:\VTS\" mkdir "C:\VTS"

You can also expand the code to replace any missing expected files.

if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"
9
votes

This should work for you:

IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder

However, there is another method, but it may not be 100% useful:

md \path\to\your\folder >NUL 2>NUL

This one creates the folder, but does not show the error output if folder exists. I highly recommend that you use the first one. The second one is if you have problems with the other.

1
votes

You need to create a folder if it doesn't exist eh? Well, here is an example of how to do it.

First, I check to see if the folder doesn't already exist by entering this code:

if not exist "FOLDERPATH" ( 
    mkdir "FOLDERPATH"
)

So if I run the code. And if the folder already exists, It will do nothing. This is what we do if the folder already exists:

if exist "FOLDERPATH" ( 
    rmdir /s /q "FOLDERPATH"
    mkdir "FOLDERPATH"
)

Now if I run the code, It will re-create the folder if it already exists. This is the example code:

@echo off
cls

if not exist "C:\ExamplePath\" ( 
    echo Creating Folder...
    mkdir "C:\ExamplePath\"
    pause
)

if exist "C:\ExamplePath\" ( 
    echo Re-Creating Folder...
    rmdir /s /q "C:\ExamplePath"
    pause
)

Now the if exist part is Optional. If the folder already exists, you can jump to an label instead like this:

if exist "FOLDERPATH" ( 
    goto :ExampleLabel
    
    :ExampleLabel
    echo Hi.
    pause
)

Hopefully, this could help with your problem.

0
votes

i created this for my script I use in my work for eyebeam.

:CREATES A CHECK VARIABLE

set lookup=0

:CHECKS IF THE FOLDER ALREADY EXIST"

IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)

:IF CHECK is still 0 which means does not exist. It creates the folder

IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"
0
votes

Personally, I would do this:

if not exist "C:\YourFolderPathHere\" (
mkdir C:\YourFolderPathHere\
) else (
echo Folder already exists!
)

Let's break that down:

  1. if not exist "C:\YourFolderPathHere\": this checks for the folder. The Backslash (\) after the path is very important, else it will look for a file rather than a folder.
  2. mkdir C:\YourFolderPathHere\: Creates the directory if the above statement is true.
  3. echo Folder already exists!: prints to console that it already exists.

Here's the same code, but with comments:

::Does the folder exist?
if not exist "C:\YourFolderPathHere\" (
::No, make it.
mkdir C:\YourFolderPathHere\
) else (
::Yes, don't make it, and print text.
echo Folder already exists!
)

One-line version: if not exist "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\

Haven't tested it though, so don't quote me on it.