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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
@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.
Use a backslash, not forward slash:
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
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>
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>
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 ) ) )
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 )
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
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