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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jpfulton248
Posts: 3
Joined: 20 Nov 2018 07:28

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

#1 Post by jpfulton248 » 07 Dec 2018 08:29

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

jpfulton248
Posts: 3
Joined: 20 Nov 2018 07:28

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

#2 Post by jpfulton248 » 07 Dec 2018 10:08

Suddenly working again now. Very frustrating. I'll keep my eye on it and report back.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#3 Post by Ed Dyreen » 07 Dec 2018 15:20

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 %.

Post Reply