Page 1 of 1

Getting "The syntax of the command is incorrect" error when running batch

Posted: 07 Dec 2018 08:29
by jpfulton248
I have a batch file that I believe was previously working (and it currently works on a different computer). When I run it I get the following error: "The syntax of the command is incorrect". Any help much appreciated.

Code: Select all

@echo off
set hr=%time:~0,2%
if exist "Archive*.tib" (
if "%hr:~0,1%" equ " " set hr=0%hr:~1,1%
rename "Archive*.tib" Backup_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%_%hr%_%time:~3,2%_%time:~6,2%.tib )
start C:\DeltaCopy\deltac.exe localbak.dcp
exit 0

Re: Getting "The syntax of the command is incorrect" error when running batch

Posted: 07 Dec 2018 10:08
by jpfulton248
Suddenly working again now. Very frustrating. I'll keep my eye on it and report back.

Re: Getting "The syntax of the command is incorrect" error when running batch

Posted: 07 Dec 2018 15:20
by Ed Dyreen

Code: Select all

@echo off
set "hr=%time:~0,2%"
if exist "Archive*.tib" if "%hr:~0,1%" equ " " set "hr=0%hr:~1,1%"
if exist "Archive*.tib" (
	echo. ren "Archive*.tib" "Backup_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%_%hr%_%time:~3,2%_%time:~6,2%.tib"
)
echo. start "" "C:\DeltaCopy\deltac.exe" localbak.dcp
pause
exit 0
you can't evaluate variables in a code block that get assigned within that ( block ) using %immediate expansion%. Their values are substituted before the ( block ) is executed. Hence the doubling of if exist "Archive*.tib" command.

Other options are call echo.variable=%%variable%%_ and setlocal enableDelayedExpansion &echo.variable=!variable!_

call echo.variable=%%variable%%_ is a trick, the block is read and variables are substituted %% becomes %, then call is used to trigger another substitution %variable% now we get the value assigned inside the block.

With delayedExpansionEnabled you explicitly tell dos a variable is delayed by using ! instead of %.