2017-08-12 124 views
0

我写下面的批处理脚本。我无法弄清楚它为什么投掷该命令的语法不正确。为什么我得到这个错误? - “命令的语法不正确。”

@echo off  
:: ============================================================================ 
:: Windows batch script to take a .zip backup of configured directories 
:: ---------------------------------------------------------------------------- 
call :getDateTimeISO8601 ISO8601_DateTime 
SET EXECUTION_START_TIME=%ISO8601_DateTime:~0,13%.%ISO8601_DateTime:~14,2%.%ISO8601_DateTime:~17,2% 

SET LOG_FILE=%~n0 [%EXECUTION_START_TIME%].log 

:: ---------------------------------------------------------------------------- 
:: Configure backup directory and associated backup locations here 
:: ---------------------------------------------------------------------------- 
SET BACK_UP_DIRS[1].SourceDir="%USERPROFILE%\Documents\My Received Files" 
SET BACK_UP_DIRS[1].BackupLocation=".\backup" 
SET BACK_UP_DIRS[2].SourceDir="%USERPROFILE%\Documents" 
SET BACK_UP_DIRS[2].BackupLocation=".\backup" 

SET [email protected] 
SET MAIL_SUBJECT=Backup Script Execution Report From %COMPUTERNAME%\%USERNAME% at %EXECUTION_START_TIME% 
SET MAIL_SIGNATURE=^<b^>^<br/^>Thanks,^<br/^> Test Automation Team.^</b^> 


:: ---------------------------------------------------------------------------- 
:: -- Executable Code       [Note: Don't touch below here] 
:: ---------------------------------------------------------------------------- 

setlocal EnableDelayedExpansion 
    call :log Execution started at %EXECUTION_START_TIME% 
    SET MailMessageHTML=^<p^>Hi,^</p^> 
    SET MailMessageHTML=!MailMessageHTML!^<p^>The backup script execution summary from %COMPUTERNAME%\%USERNAME% is below. Please find the detailed log attached.^</p^> 
    SET MailMessageHTML=!MailMessageHTML!^<ul^> 

    SET SCRIPT_DIR=%~dp0 

    call :len BACK_UP_DIRS length 

    for /l %%i in (1,1,%length%) do (
     call :log Processing... %%i of %length% 
     if defined BACK_UP_DIRS[%%i] do (
      call :absolute_path_from_relative !BACK_UP_DIRS[%%i].SourceDir! sourceDir 
      call :absolute_path_from_relative !BACK_UP_DIRS[%%i].BackupLocation! backupLocation 
      call :get_name_only "!sourceDir!" sourceDirName 

      set backupZipFile=!backupLocation!\!sourceDirName! [!EXECUTION_START_TIME!].zip 

      ::call :runZip "!sourceDir!" "!backupZipFile!" 1>>"%LOG_FILE%" 

      SET MailMessageHTML=!MailMessageHTML!^<li^>[%date% %time%] The directory '^<em^>!sourceDir!^</em^>' is backed up as '^<em^>!backupZipFile!^</em^>'.^</li^> 

     ) 
    ) 
    SET MailMessageHTML=!MailMessageHTML!^</ul^> 

    cd %SCRIPT_DIR% 
    SET BACK_UP_DIRS= 
    call :log Execution completed at %date% %time% 

    ::call :sendEmail "!MailMessageHTML!" 
    del "%LOG_FILE%" 
endlocal 
goto :EOF 

::----------------------------------------------------------------------------- 
::-- Function section starts here 
::----------------------------------------------------------------------------- 

:len <inputArrayName> <outputLength> 
SETLOCAL 
    set arrayName=%1 
    set arrayIndex=0 
    for /f "delims=[=] tokens=2" %%a in ('set %arrayName%[') do (
     set arrayIndex=%%a 
    ) 
(ENDLOCAL 
    SET "%~2=%arrayIndex%" 
) 
goto :EOF 

:absolute_path_from_relative <inputRelativePath> <outputAbsolutePath> 
SETLOCAL 
    set absolutePath=%~f1 
(ENDLOCAL 
    set "%~2=%absolutePath%" 
) 
goto :EOF 

:get_directory_path <inputFilePath> <outputDirectoryPath> 
SETLOCAL 
    set dirPath=%~dp1 
(ENDLOCAL 
    set %~2=%dirPath% 
) 
goto :EOF 

:get_name_only <inputPath> <outputName> 
SETLOCAL 
    set name=%~nx1 
(ENDLOCAL 
    set %~2=%name% 
) 
goto :EOF 

:runZip <sourceDir> <backupZipFile> 
SETLOCAL 
    call :log Taking backup of '%~1' to '%~2' 
    if not exist "%~dp2" mkdir "%~dp2" 
    cd /d "%~1" 
    call %ZIP_EXE% -dcR9 "%~2" * 
    cd /d %~dp0 
    call :log Completed backup of '%~1' to '%~2' 
    echo. 
ENDLOCAL 
goto :EOF 

:sendEmail <message> 
SETLOCAL 
    echo Sending Email.. 
    ::echo Message: %~1 
    call %BLAT_EXE% -server %SMTP_MAIL_SERVER% -port %SMTP_PORT% -f %MAIL_SENDER% -from %MAIL_SENDER% -to %MAIL_NOTIFICATION_TO% -html -subject "%MAIL_SUBJECT%" -body "%~1 %MAIL_SIGNATURE%" -attach "%LOG_FILE%" 
    echo Email Sent. 
ENDLOCAL 
goto :EOF 

:getDateTimeISO8601 <outputValue> 
SETLOCAL 
    :: Check WMIC is available 
    WMIC.EXE Alias /? >NUL 2>&1 || GOTO :no_wmic 

    :: Use WMIC to retrieve date and time 
    FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
     IF "%%~L"=="" goto s_done 
      SET _yyyy=%%L 
      SET _mm=00%%J 
      SET _dd=00%%G 
      SET _hour=00%%H 
      SET _minute=00%%I 
      SET _second=00%%K 
    ) 
    :s_done 

    :: Pad digits with leading zeros 
      SET _mm=%_mm:~-2% 
      SET _dd=%_dd:~-2% 
      SET _hour=%_hour:~-2% 
      SET _minute=%_minute:~-2% 
      SET _second=%_second:~-2% 

    :: Display the date/time in ISO 8601 format: 
    SET _isodate=%_yyyy%-%_mm%-%_dd% %_hour%:%_minute%:%_second% 
    goto :functionDone 

    :no_wmic 
    echo WMIC not available on this machine. 
    Set _isodate=%date% %time% 
    :functionDone 
(ENDLOCAL 
    SET "%~1=%_isodate%" 
) 
goto :EOF 

:log <message> 
SETLOCAL 
    set message=%~n0 [%date% %time%] %* 
    if "%LOG_FILE%"=="" goto :console 
     echo %message% >> "%LOG_FILE%" 
    :console 
    echo %message% 
ENDLOCAL 
goto :EOF 

我在功能len年底收到错误在CMD正在处理goto :EOF

:len <inputArrayName> <outputLength> 
SETLOCAL 
    set arrayName=%1 
    set arrayIndex=0 
    for /f "delims=[=] tokens=2" %%a in ('set %arrayName%[') do (
     set arrayIndex=%%a 
    ) 
(ENDLOCAL 
    SET "%~2=%arrayIndex%" 
) 
goto :EOF 

请问你能帮我弄清楚,这里出了什么问题。

D:\Work\shell-scripts\shell-scripts>goto :EOF 
The syntax of the command is incorrect. 
+2

@ Jean-FrançoisFabre - 'goto:EOF'不仅是正确的,这是这样做的首选方法(当然不是'exit/b')。 – SomethingDark

+0

这些日子还不够。或者是更好的?抱歉 –

回答

2

该问题源于您评论的方式call :runzip

::在技术上是一个标签,批量不允许您在代码块(if语句,for循环以及括号内的任何其他内容)中包含标签。要解决此问题,请使用REM在这些情况下注释行。

REM call :runZip "!sourceDir!" "!backupZipFile!" 1>>"%LOG_FILE%"