作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在下面写了批处理脚本。我无法弄清楚为什么会抛出命令的语法不正确。
@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 MAIL_NOTIFICATION_TO=nitesh@bbd.co.za
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.
最佳答案
问题源于您注释掉 call :runzip
的方式.::
从技术上讲,它是一个标签,批处理不允许您在代码块内使用标签(if
语句、for
循环以及括号内的任何其他内容)。要解决此问题,请使用 REM
在这些情况下注释掉行。
REM call :runZip "!sourceDir!" "!backupZipFile!" 1>>"%LOG_FILE%"
关于batch-file - 为什么我会收到此错误? - "The syntax of the command is incorrect.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650915/
我是一名优秀的程序员,十分优秀!