IF EXIST C: \ directory \ goto a else goto b problems windows XP batch files

whenever I run the code below, this happens to me, I made a mistake using the if exist lines, as if the directory exists or not, it acts as if the line was never there ... either or it does not read 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 
+4
source share
6 answers

Use parentheses to group individual branches:

 IF EXIST D:\RPS_BACKUP\backups_to_zip\ (goto zipexist) else goto zipexistcontinue 

In your case, the parser will never see the else belonging to if , because goto will gladly accept everything until the end of the command. A similar problem can be seen when using echo instead of goto .

In addition, using parentheses will allow you to directly use instructions without having to skip (although I could not rewrite the code to actually use structured programming methods, it may be too early or it does not give block structures, because the code is right now).

+14
source

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

 IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists goto tempexistscontinue 
+3
source

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
+2
source

Is there an ELSE in DOS? Back in the days when I was doing more of this kind, there wasn’t.

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

 IF NOT EXIST file GOTO label 

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

Secondly, I vaguely remember some kind of error with checking for directories. Life would be easier if you could check for a file in this directory. If there is no file that you can be sure of, try something (to work with Win95, IIRC) would add the NUL device NUL name to your directory name, for example.

 IF NOT EXIST C:\dir\NUL GOTO ... 
+1
source
 @echo off rem learn how to spell rem this is another method :START rmdir temperary cls IF EXIST "temperary\." (echo The temperary directory exists) else echo The temperary directory doesn't exist echo. dir temperary /A:D pause echo. echo. echo Note the directory is not found echo. echo Press any key to make a temperary directory, cls, and test again pause Mkdir temperary cls IF EXIST "temperary\." (echo The temperary directory exists) else echo The temperary directory doesn't exist echo. dir temperary /A:D pause echo. echo press any key to goto START and remove temperary directory pause goto START 
0
source

To check for DIRECTORIES, you should not use something like:

 if exist c:\windows\ 

For proper operation use:

 if exist c:\windows\\. 

Pay attention to ".". in the end.

0
source

Source: https://habr.com/ru/post/1315771/


All Articles