开发者

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\开发者_StackOverflow社区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  


Use parentheses to group the individual branches:

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

In your case the parser won't ever see the else belonging to the if because goto will happily accept everything up to the end of the command. You can see a similar issue when using echo instead of goto.

Also using parentheses will allow you to use the statements directly without having to jump around (although I wasn't able to rewrite your code to actually use structured programming techniques; maybe it's too early or it doesn't lend itself well to block structures as the code is right now).


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  


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


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 ...


@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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜