[if-statement] IF EXIST C:\directory\ goto a else goto b problems windows XP batch files

whenever i run the code below it occurs to me I have made a mistake using the if exist lines, as no matter whether the directory exists or not, it acts as if the line was never there... either that or its not reading the else line.


echo off  
echo  
echo (c) Ryan Leach 2010  
echo Stockmaster Backup System for exclusive use of Riverland Paper Supplies  
echo  
echo Please ensure that all computers are out of stock master to the windows xp screen  
echo and that the backup usb with the day of the week labeled on it is inserted  

pause  

IF EXIST D:\RPS_BACKUP\backups_to_zip\ goto zipexist else goto zipexistcontinue  
:zipexist  
IF EXIST d:\RPS_BACKUP\backups_old\ rd /s /q D:\RPS_BACKUP\backups_old  
echo backup did not complete last time, backup will restart from zip-usb phase.  
pause  
call zip  
goto tidyup  
:zipexistcontinue  

IF EXIST D:\RPS_BACKUP\backups_old\ goto oldexists else oldexistscontinue  
:oldexists  
IF EXIST d:\RPS_BACKUP\backup_temp\ rename D:\RPS_BACKUP\backups_temp backups_to_zip  
rd /s /q D:\RPS_BACKUP\backups_old  
echo backup did not complete last time, backup will restart at the zip to usb phase.  
pause  
call zip  
goto tidyup  
:oldexistscontinue  

IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists else goto tempexistscontinue  
:tempexists  
IF EXIST D:\RPS_BACKUP\backups_old\ goto backupfailed else goto tempexistscontinue  
:backupfailed  
@rd /s /q D:\RPS_BACKUP\backups_temp  
echo backup did not complete last time, backup will restart from start.  
pause  
:tempexistscontinue  

md D:\RPS_BACKUPS\backups_temp  
xcopy \\user1\c\* D:\RPS_BACKUP\backups_temp\user1\c /h /e /z /f /r /i /s /k  
IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
xcopy C:\* D:\RPS_BACKUP\backups_temp\user2\c /h /e /f /r /i /s /k  
IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
xcopy \\user3\c\* D:\RPS_BACKUP\backups_temp\user3\c /h /e /z /f /r /i /s /k  
IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
call sub  
call zip  
:tidyup  
rename D:\RPS_BACKUP\backups_to_zip backups  
pause  
goto :eof  

:ErrorHandler  
echo xcopyerrorcode is ERRORLEVEL contact ryan  
pause  

This question is related to if-statement batch-file windows-xp

The answer is


There's an ELSE in the DOS batch language? Back in the days when I did more of this kinda thing, there wasn't.

If my theory is correct and your ELSE is being ignored, you may be better off doing

IF NOT EXIST file GOTO label

...which will also save you a line of code (the one right after your IF).

Second, I vaguely remember some kind of bug with testing for the existence of directories. Life would be easier if you could test for the existence of a file in that directory. If there's no file you can be sure of, something to try (this used to work up to Win95, IIRC) would be to append the device file name NUL to your directory name, e.g.

IF NOT EXIST C:\dir\NUL GOTO ...

To check for DIRECTORIES you should not use something like:

if exist c:\windows\

To work properly use:

if exist c:\windows\\.

note the "." at the end.


From the help (if /?):

The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

The following would NOT work because the del command needs to be terminated
by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing

If you want to rule out any problems with the else part, try removing the else and place the command on a new line. Like this:

IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists
goto tempexistscontinue  

@echo off

:START
rmdir temporary
cls
IF EXIST "temporary\." (echo The temporary directory exists) else echo The temporary directory doesn't exist
echo.
dir temporary /A:D
pause

echo.
echo.
echo Note the directory is not found
echo.
echo Press any key to make a temporary directory, cls, and test again
pause

Mkdir temporary
cls
IF EXIST "temporary\." (echo The temporary directory exists) else echo The temporary directory doesn't exist
echo.
dir temporary /A:D
pause
echo.
echo press any key to goto START and remove temporary directory 
pause 

goto START

Examples related to if-statement

How to use *ngIf else? SQL Server IF EXISTS THEN 1 ELSE 2 What is a good practice to check if an environmental variable exists or not? Using OR operator in a jquery if statement R multiple conditions in if statement Syntax for an If statement using a boolean How to have multiple conditions for one if statement in python Ifelse statement in R with multiple conditions If strings starts with in PowerShell Multiple conditions in an IF statement in Excel VBA

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)

Examples related to windows-xp

installing JDK8 on Windows XP - advapi32.dll error How to Create a script via batch file that will uninstall a program if it was installed on windows 7 64-bit or 32-bit How do I find files with a path length greater than 260 characters in Windows? Apache Maven install "'mvn' not recognized as an internal or external command" after setting OS environmental variables? How to access share folder in virtualbox. Host Win7, Guest Fedora 16? How to create ls in windows command prompt? How to fix an UnsatisfiedLinkError (Can't find dependent libraries) in a JNI project How to use random in BATCH script? How to delete or change directory of a cloned git repository on a local computer How do I manually create a file with a . (dot) prefix in Windows? For example, .htaccess