How to check if a directory exists in Windows?

@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.


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)

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