13

I'm trying to do the 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 the following code:

  @ECHO ON

IF EXIST "C:\Users\user\Desktop\folder1\" (GOTO MOVER)

PRINT "It doesn't exists - This is just for debugging"
PAUSE

:MOVER
ECHO "MOVER"
PAUSE
EXIT
:END

But the system does not print the test words.

2
  • Are you trying to print to the console, or to paper? The PRINT command may not be the one you're looking for, try ECHO.
    – Οurous
    Commented Apr 29, 2015 at 0:00
  • That's just to debug the code and to be sure that the system is executing that. I'm plannig to replace it with the commands MKDIR, MOVE and RENAME. Commented Apr 29, 2015 at 14:09

2 Answers 2

11

IF EXIST checks only if a file exists and cannot check folders. Usually, you test like this

IF NOT EXIST "myfolder\NUL" mkdir "myfolder"

The pseudo device NUL acts like a file and does in fact exist in every folder. Note the spelling.

But I have seen that test fail in batchfiles, for unknown reasons. So I suggest this instead:

CD myfolder 2>NUL && CD .. || MD myfolder

CD myfolder tries a legal operation with the folder and the conditional execution of MD/MKDIR creates the folder only if that operation fails. CD .. reverts the action if the folder should exist. 2>NUL suppresses the error message if the folder does not exist.

edit: Apparantly there is a simpler method for testing: append a backslash (\) to the foldername to make it syntactically a folder, like this:

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

This will create the folder "myfolder" if it does not yet exist. Additionally, in case there is a file named "myfolder" the MD will fail and the batch file will be terminated after the error message is displayed. Also, ERRORLEVEL will be set. I like this more as the error output doesn't have to be redirected.

edit: If you want to execute several commands, run them in a subshell, that is, enclose them in parantheses like this:

if not exist myfolder\ (  
    md myfolder
    dir myfolder
    REM ...or run any other commands
) || goto :EOF  
5
  • So, if I need to execute several commands... how would it be? Commented Apr 29, 2015 at 15:15
  • @Julian: I've edited my answer to show you how to execute a group of commands. Commented Apr 30, 2015 at 18:40
  • 1
    if exist cannot check folders? Try 'if exist %windir% echo yes`
    – Stephan
    Commented Jan 14, 2016 at 12:35
  • IF EXIST does check folders as well as files. If you want to check for the existence of a particular folder (and not a file of the same name) then use foldername` in the IF` clause. If you check for a file, check that no folder of the same name exists and then check for the file's name. Commented Jan 15, 2016 at 13:12
  • If you want to check if there is no file system entry named foo, you can check with IF NOT EXIST foo. But if you want to check if foo is a directory, a test like IF EXIST foo\ doesn't help, because this is also true if foo is a file. Same for IF EXIST foo\nul. Use IF EXIST foo* to check for a directory.
    – Rolf
    Commented Feb 24, 2023 at 10:07
1

IF EXIST can check for folders I use it this way:

IF EXIST %local_path%\opensource\eclipse rmdir %local_path%\opensource\eclipse /s /q >nul

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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