Windows mkdir if not exists

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.

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.

I would like to translate this Linux/Bash script to Windows shell:

if test -d myDirName; then echo "ok"; else mkdir myDirName; fi

It tests if a directory exists, and if it doesn’t it creates it.

studiohack's user avatar

studiohack

13.5k19 gold badges88 silver badges118 bronze badges

asked Dec 6, 2010 at 16:36

Pietro's user avatar

1

@echo off
IF exist myDirName ( echo myDirName exists ) ELSE ( mkdir myDirName && echo myDirName created)

Added by Barlop

While the above works for this particular situation, the title says about testing specifically for a directory. Phogg’s comment using if exist mydirname\ rather than if exist mydirname is the way. Some answers have used \nul but \nul is problematic in NT. Not including a trailing backslash will test for a file or a directory. So, for a directory, include the trailing backslash.

barlop's user avatar

barlop

23.5k43 gold badges146 silver badges226 bronze badges

answered Dec 6, 2010 at 16:47

Sathyajith Bhat's user avatar

Sathyajith BhatSathyajith Bhat

61.6k38 gold badges180 silver badges264 bronze badges

6

Here is what I just found out:

You can test if a nul file exists; if the directory exists it will contain a nul file, if the nul file does not exist then the directory does not exist.

IF exist myDirName/nul ( echo myDirName exists ) ELSE ( mkdir myDirName && echo myDirName created)

Sathyajith Bhat's user avatar

answered Dec 6, 2011 at 11:52

dev008's user avatar

dev008dev008

2312 silver badges2 bronze badges

6

Use a backslash, not forward slash: myDirName\nul not myDirName/nul

md foo 
echo.>bar 
for %I in (foo bar xyz) do @( 
  if exist %I ( 
    if exist %I\nul ( 
      echo -- %I is a directory 
    ) else ( 
      echo -- %I is a file 
    ) 
  ) else ( 
    echo -- %I does not exist 
  ) 
)

— foo is a directory
— bar is a file
— xyz does not exist

edit: this only works if directory name does not contain spaces

Community's user avatar

answered May 12, 2012 at 15:36

DVF's user avatar

DVFDVF

1411 silver badge3 bronze badges

Some have suggested doing \nul, but that doesn’t seem to work reliably in NT

C:\blah>md abc

C:\blah>if exist abc\nul echo yes
yes

C:\blah>if exist "abc\nul" echo yes

C:\blah>

http://www.dostips.com/forum/viewtopic.php?f=3&t=4913

foxidrive writes-

The trick with nul worked in pre NT versions of windows.

Now you would use this, with a trailing backslash.

if exist "C:\abcde\" echo the folder exists

Re the question

C:\blah>if exist "abcd\" (echo yes) else (echo no && mkdir abcd)
no

C:\blah>if exist "abcd\" (echo yes) else (echo no && mkdir abcd)
yes

C:\blah>

answered Jun 3, 2014 at 11:10

barlop's user avatar

barlopbarlop

23.5k43 gold badges146 silver badges226 bronze badges

I wondered why joe had a downvote as I was experiencing the same kind of problem on Windows 7, namely that

IF EXIST filename\NUL

was returning TRUE for both files and directories.
I found an alternative solution at www.robvanderwoude.com/battech_ifexistfolder.php and came up with a revised version of DVF’s FOR loop:

FOR %I in (foo bar xyz) DO @( PUSHD %I && (POPD & echo -- %I is a directory) || ( IF exist %I ( echo -- %I is a file ) ELSE ( echo -- %I does not exist ) ) )

answered Oct 11, 2013 at 11:29

Damian's user avatar

2

I see many have problems with differentiating between files and folders. Has anyone tried to simply cd into it after checking it exists? Then cd will either succeed or fail.

set MyDir = "MyTestDir"
IF exist MyDir (
    cd MyDir 
    IF ERRORLEVEL NEQ 0 (
        echo "Error: %MyDir% already exists, but it is a file!"
        exit 1
    ) else (
        # Do not forget to cd back
        cd ..
    )
) else (
   # Did not exist yet, so create it.
   mkdir MyDir
)

answered Jun 3, 2014 at 9:50

Richard Rombouts's user avatar

1

Here’s a way to test the directory exists only when its a valid directory.

set myDirName=foobitybarbitybaz
dir /A:D %myDirName% >nul 2>&1
if ERRORLEVEL 1 (@mkdir %myDirName%) else (@echo "ok")

answered Aug 3, 2021 at 14:29

Mason M.'s user avatar

exist myDirName/nul

also is true if myDirName is a file, whis is not the searched functionality

answered May 4, 2012 at 10:41

Joe's user avatar

1

I prefer using dir /d | findstr \[\.\.\]:

2>nul dir /d "myDirName" | findstr \[\.\.\] >nul && echo= "ok" || mkDir myDirName&&echo= Is NOT dir


2>nul dir /d "myDirName" | findstr \[\.\.\] >nul && echo= "ok" || mkDir myDirName


For create if exist or not and for ignore error…

2>nul mkDir myDirName

Also …

if exist "myDirName\." (echo/"ok") else mkdir myDirName

rem :: or direct create myDirName hidden/ignoring error
       2>nul mkdir myDirName & if exist "myDirName\." echo/ exist 
rem :: output: exist 

answered Nov 9, 2019 at 14:56

Io-oI's user avatar

Io-oIIo-oI

7,7453 gold badges12 silver badges41 bronze badges

Finding a folder shouldn’t be this difficult. My solution, use perl:

for($cnt=$#ARGV; $cnt>=0; --$cnt)
{
   if ( -d "$ARGV[$cnt]" ) { 
      print "argv[$cnt]=$ARGV[$cnt] is a folder\n";
      $dir = $ARGV[$cnt];
      break;
   } else {
      print "argv[$cnt]=$ARGV[$cnt] is Not a folder\n";
   }
}

answered Aug 24, 2018 at 18:31

user937229's user avatar

1

You must log in to answer this question.

Not the answer you’re looking for? Browse other questions tagged

.

  • Remove From My Forums
  • Question

  • Hi guys,

    I’m back again with another simple question. but honestly I gave a try myself, but i get confused somewhere! or maybe batch file is not suitable!

    I want to search each drive C: D: E: and F: and to see if HHH\ folder exists. if exists I create GGG folder in it and if not search the next drive. but I also want my script to stop searching if it hits the HHH\ in the first drive. if the HHH folder is not
    in any drive then I want to create it in D: only and create GGG in it. 

    below I am trying to write like this: is this true?

    @echo off
    set plsc=c:\HHH
    set pdsc=c:\HHH\GGG
    set plsd=d:\HHH
    set pdsd=d»\HHH\GGG
    set plse=e:\HHH
    set pdse=e»\HHH\GGG
    set plsf=f:\HHH
    set pdsf=f»\HHH\GGG

    c:
    cd \

    if not exist %plsc% (
        H:
    ) else (
        mkdir %pdsc%
    )
    :: if …

    Regards,

    Messi

Answers

  • One extra line should do the trick. To be able to build on this code you should analyse it line by line until you fully understand what each line does.

    @echo off
    set Drives=C: D: E: F:
    set plsc=HHH
    set pdsc=HHH\GGG

    for %%a in (%Drives%) do (
      if exist %%a\%plsc% (
         echo md %%a\%pdsc%
         goto Next
      )
    )
    echo md D:\%pdsc%

    :Next

    • Marked as answer by

      Tuesday, December 28, 2010 1:41 PM

If you want to create a folder named VTS on the C:\ drive using a Windows batch script (*.bat) but only if that folder doesn’t already exist, you can use a variety of methods. These methods will ensure that the contents of the folder won’t be overwritten if the batch is executed and the folder already exists.

Method 1: Simple IF Statement

You can use the IF statement and check for the existence of the folder before creating it:

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

This line of code will create the directory only if the folder does not exist.

Method 2: Microsoft Support Recommendation

Another method suggested by Microsoft Support involves checking for the existence of the folder and outputting a message accordingly:

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

You may refer to this archived Microsoft Support article for more details.

Method 3: Unconditional MKDIR

If your script doesn’t care about the error level, you can just call mkdir unconditionally:

This way, it will simply report that the subdirectory already exists if the folder exists, fulfilling the needs of creating the folder if it doesn’t exist and avoiding overwriting an existing folder’s contents.

Method 4: Redirect to NUL

Create a folder called VTS and output any error messages to NUL:

Or you can change the drive letter to C: first, then use mkdir and output the error to NUL:

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

Method 5: Using Variables

You can also use a variable to define the folder name and create it if it doesn’t exist:

set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)

By using these methods, you can create a folder in a Windows batch script only if it doesn’t already exist, ensuring the contents of the folder will not be overwritten if the batch is executed.

  • Windows mobile достоинства и недостатки
  • Windows movie maker для windows 10 на русском языке скачать бесплатно торрент
  • Windows mobile emulator for windows 10 mobile
  • Windows movie maker для windows 7 скачать с официального сайта
  • Windows mobile sdk for windows 10