使用IF ELSE

时间:2017-09-25 18:04:04

标签: windows batch-file syntax syntax-error

我正在编写这个脚本,一切都运行得很完美,直到我添加if else部分......然后它返回命令的语法不正确

如果我将其移除,那就有效了。如果我尝试保留if else并将内部的锥体更改为只是回声,poof也可以。所以我不确定可能是什么问题以及如何正确调试它......

@echo off

:: The only thing that normally needs to be changed is the Type variable. It should match the folder name where the .bak files are stored.
set Type=DAILY

set Datum=%date:~0,4%%date:~5,2%%date:~8,2%
set Folder=C:\SQLBKP\
set Flag=%Folder%\%Type%\backup_ready.flg
set ZIPFILE=%Folder%\%Type%-backup-%Datum%.zip
set SrcFolder=%Folder%\%Type%
set Passphrase=passwd
set BUCKET=bucket
@echo on

IF exist %Flag% (
    :start

    cd %Folder%

    del %ZIPFILE%
    del %ZIPFILE%.gpg

    "c:\Program Files\7-Zip\7z.exe" a %ZIPFILE% "%SrcFolder%"

    "c:\Program Files\7-Zip\7z.exe" t %ZIPFILE% | FIND "Everything is Ok" && goto continue || blat -to backupalerts@foo.com -server mail.protection.outlook.com -f backupalerts@foo.com -subject "%Type% backup integrity check failed on %computername%" -body "This script detected that the %Type% integrity check of the zipped folder failed on the machine %computername%."
    exit

    :continue
    @gpg --batch --passphrase %Passphrase% -c %ZIPFILE%

    aws s3 cp %ZIPFILE%.gpg s3://%BUCKET%/prefix/ || blat -to backupalerts@foo.com -server mail.protection.outlook.com -f backupalerts@foo.com -subject "%Type% backup failed on %computername%" -body "This script detected that the %Type% upload of the zipped folder to the S3 bucket failed on the machine %computername%." & exit

    del %Folder%\%Type%\*.bak
    del %ZIPFILE%
    del %ZIPFILE%.gpg

    exit

) ELSE (
    timeout /t 600 

    goto start
)

提前谢谢!

编辑:正如Margoo所建议的那样,我可以通过删除if代码块中的所有代码并使用CALL :label调用它来修复它。

现在看起来像这样:

goto ifelse

:start
cd %Folder%

del %ZIPFILE%
del %ZIPFILE%.gpg

"c:\Program Files\7-Zip\7z.exe" a %ZIPFILE% "%SrcFolder%"

"c:\Program Files\7-Zip\7z.exe" t %ZIPFILE% | FIND "Everything is Ok" && goto continue || blat -to backupalerts@foo.com -server mail.protection.outlook.com -f backupalerts@foo.com -subject "%Type% backup integrity check failed on %computername%" -body "This script detected that the %Type% integrity check of the zipped folder failed on the machine %computername%."
exit

:continue
@gpg --batch --passphrase %Passphrase% -c %ZIPFILE%

aws s3 cp %ZIPFILE%.gpg s3://%BUCKET%/prefix/ || blat -to backupalerts@foo.com -server mail.protection.outlook.com -f backupalerts@foo.com -subject "%Type% backup failed on %computername%" -body "This script detected that the %Type% upload of the zipped folder to the S3 bucket failed on the machine %computername%." & exit

del %Folder%\%Type%\*.bak
del %ZIPFILE%
del %ZIPFILE%.gpg

exit


:ifelse
IF exist %Flag% (
    call :begin

) ELSE (
    timeout /t 600 
    call :begin
)

0 个答案:

没有答案