Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
The actual linux/bash command would simply be: mkdir -p myDirName. After spending days trying to translate relatively simple bash-scripts into batch-sh*t (assigning the result of a function to a variable?), I've decided to just make people install cygwin. –  michael_n Mar 30 '13 at 11:00
add comment

5 Answers

up vote 34 down vote accepted
@echo off
IF exist myDirName ( echo myDirName exists ) ELSE ( mkdir myDirName && echo myDirName created)
share|improve this answer
This is correct for testing file existence, but how do you know that it's a directory? The simplest answer is if exist mydirname\ and the rest as you say. Alternately, you could actually get a test binary and use it on Windows. –  phogg Dec 6 '10 at 16:58
@phogg: In the context of this specific question: if it exists but is a file, you still cannot mkdir it. –  grawity Dec 6 '11 at 13:09
@grawity: If it exists but is a file the script will probably fail later when trying to use it as a directory, which probably isn't what you want. –  phogg Dec 6 '11 at 13:30
how can i check if not exist? –  sunglim Jan 9 at 6:43
add comment

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)
share|improve this answer
+1 - this works in all Windows and MS-DOS versions, unlike plain if exist dirname which appears to be specific to Windows NT. –  grawity Dec 6 '11 at 13:09
that's wAS a stupid code compare to linux mkdir -p myDirName :p –  Kit Ho Jan 4 '13 at 9:35
It should be a backslash, not a forward slash. –  Nathan Garabedian Feb 1 '13 at 23:08
add comment

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

md foo 
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

share|improve this answer
add comment

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 ) ) )
share|improve this answer
add comment
exist myDirName/nul

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

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.