Reputation: 96
I wrote below batch script. I am not able to figure out why it is throwing The syntax of the command is incorrect.
@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
I am getting error at the end of function len
when cmd is processing 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
Can you please help me figure out, what is going wrong here.
D:\Work\shell-scripts\shell-scripts>goto :EOF
The syntax of the command is incorrect.
Upvotes: 0
Views: 778
Reputation: 14304
The problem stems from the way you commented out call :runzip
.
::
is technically a label, and batch doesn't allow you to have labels inside of code blocks (if
statements, for
loops, and anything else inside of parentheses). To get around this, use REM
to comment out lines in these situations.
REM call :runZip "!sourceDir!" "!backupZipFile!" 1>>"%LOG_FILE%"
Upvotes: 2