if statement - Batch files: If directory exists, do something -


i'm trying following:

if exists ("c:\users\user\desktop\folder1\") {  move "c:\users\user\desktop\folder2\" "c:\users\user\desktop\folder1\" rename "c:\users\user\desktop\folder1\folder2\" "folder3"  } else {  mkdir "c:\users\user\desktop\folder1\" move "c:\users\user\desktop\folder2\" "c:\users\user\desktop\folder1\" rename "c:\users\user\desktop\folder1\folder2\" "folder3" } 

with following code:

  @echo on  if exist "c:\users\user\desktop\folder1\" (goto mover)  print "it doesn't exists - debugging" pause  :mover echo "mover" pause exit :end 

but system not print test words.

if exist checks if file exists , cannot check folders. usually, test

if not exist "myfolder\nul" mkdir "myfolder" 

the pseudo device nul acts file , in fact exist in every folder. note spelling.

but have seen test fail in batchfiles, unknown reasons. suggest instead:

cd myfolder 2>nul && cd .. || md myfolder 

cd myfolder tries legal operation folder , conditional execution of md/mkdir creates folder if operation fails. cd .. reverts action if folder should exist. 2>nul suppresses error message if folder not exist.

edit: apparantly there simpler method testing: append backslash (\) foldername make syntactically folder, this:

if not exist myfolder\ md myfolder || goto :eof 

this create folder "myfolder" if not yet exist. additionally, in case there file named "myfolder" md fail , batch file terminated after error message displayed. also, errorlevel set. more error output doesn't have redirected.

edit: if want execute several commands, run them in subshell, is, enclose them in parantheses this:

if not exist myfolder\ (       md myfolder     dir myfolder     rem ...or run other commands ) || goto :eof   

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -