DOS – Batch File – Error – The Syntax of the command is incorrect.

Background

Sometimes when running batch file, one will get the error pasted below.

The syntax of the command is incorrect.

Code

Acknowledgement

Here is stolen code for getting timestamp.

Code


@ECHO OFF
REM http://snipplr.com/view/21573/print-datetime-in-dos-batch-file/
set _LOGFILE_DATE=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%
set _LOGFILE_TIME=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set _LOGFILE=log-%_LOGFILE_DATE%-%_LOGFILE_TIME%.log

set _currentFolder=%cd%
set _logFolder=%_currentFolder%\log
set _logFileFull="%_logFolder%\%_LOGFILE%"
set _logApp="D:\Program Files (x86)\GnuWin32\bin\tee.exe"

if not exist %_logFolder%(
	mkdir %_logFolder%
)

 

Diagnosis

To determine the erroring line, we need to set “ECHO ON“.

 

Code – Turn Echo ON


@ECHO ON
REM http://snipplr.com/view/21573/print-datetime-in-dos-batch-file/
set _LOGFILE_DATE=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%
set _LOGFILE_TIME=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set _LOGFILE=log-%_LOGFILE_DATE%-%_LOGFILE_TIME%.log

set _currentFolder=%cd%
set _logFolder=%_currentFolder%\log
set _logFileFull="%_logFolder%\%_LOGFILE%"
set _logApp="D:\Program Files (x86)\GnuWin32\bin\tee.exe"

if not exist %_logFolder%(
	mkdir %_logFolder%
)

Output


>REM http://snipplr.com/view/21573/print-datetime-in-dos-batch-file/

>set _LOGFILE_DATE=2016_03_31

>set _LOGFILE_TIME=14_56_59

>set _LOGFILE=log-2016_03_31-14_56_59.log

>set _currentFolder=D:\\Script\SynchData\TableDiff

>set _logFolder=D:\\Script\SynchData\TableDiff\log

>set _logFileFull="D:\\Script\SynchData\TableDiff\log\log-2016_03_31-14_56_59.log"

>set _logApp="D:\Program Files (x86)\GnuWin32\bin\tee.exe"
The syntax of the command is incorrect.
>if not exist D:\\Script\SynchData\TableDiff\log(
>

Explanation

The erroring line is the one that follows “The syntax of the command is incorrect.”

In our case, if not exist …

 

Code – On the If statement, add space before (


@ECHO ON
REM http://snipplr.com/view/21573/print-datetime-in-dos-batch-file/
set _LOGFILE_DATE=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%
set _LOGFILE_TIME=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set _LOGFILE=log-%_LOGFILE_DATE%-%_LOGFILE_TIME%.log

set _currentFolder=%cd%
set _logFolder=%_currentFolder%\log
set _logFileFull="%_logFolder%\%_LOGFILE%"
set _logApp="D:\Program Files (x86)\GnuWin32\bin\tee.exe"

if not exist %_logFolder%   (
	mkdir %_logFolder%
)

Output:


>REM REM http://snipplr.com/view/21573/print-datetime-in-dos-batch-file/

>set _LOGFILE_DATE=2016_03_31

>set _LOGFILE_TIME=15_01_43

>set _LOGFILE=log-2016_03_31-15_01_43.log

>set _currentFolder=D:\\Script\SynchData\TableDiff

>set _logFolder=D:\\Script\SynchData\TableDiff\log

>set _logFileFull="D:\\Script\SynchData\TableDiff\log\log-2016_03_31-15_01_43.log"

>set _logApp="D:\Program Files (x86)\GnuWin32\bin\tee.exe"

>if not exist D:\\Script\SynchData\TableDiff\log (mkdir D:\\Script\SynchData\TableDiff\log )
>

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s