Rya*_*ach 9 windows-xp if-statement batch-file
无论何时我运行code
下面它都发生在我身上我使用if exists行犯了一个错误,因为无论目录是否存在,它就好像该行永远不存在那样......或者它不读取其他内容线.
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
Run Code Online (Sandbox Code Playgroud)
Joe*_*oey 20
使用括号对各个分支进行分组:
IF EXIST D:\RPS_BACKUP\backups_to_zip\ (goto zipexist) else goto zipexistcontinue
Run Code Online (Sandbox Code Playgroud)
在您的情况下,解析器将永远不会看到else
属于该属性,if
因为goto
将很乐意接受一切直到命令的结尾.使用时可以看到类似的问题echo
而不是goto
.
同样使用括号将允许您直接使用语句而不必跳转(虽然我无法重写您的代码以实际使用结构化编程技术;可能它太早或者它不适合阻止结构代码就在这里).
如果要排除else
零件的任何问题,请尝试删除else
并将命令放在新行上.像这样:
IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists
goto tempexistscontinue
Run Code Online (Sandbox Code Playgroud)
在帮助(if /?
)中:
ELSE子句必须与IF之后的命令位于同一行。对于 例: 如果文件名存在。( del文件名。 )ELSE( 回显文件名。失踪。 ) 由于del命令需要终止,因此以下操作无效 通过换行符: 如果文件名存在。del文件名。ELSE回显文件名。失踪 由于ELSE命令必须位于同一行,因此以下工作也不起作用 作为IF命令的结尾: 如果文件名存在。del文件名。 ELSE回显文件名。失踪