0

Description

In an attempt to print Init has already been made to the screen if the file init.marker is newer than the files setup.py and setup.cfg using a Windows Batch Script (where the code below comes from):

FOR /F %%i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%%i
IF "%last_modified_file%"=="init.marker"(
    ECHO Init has already been made
)

I came accross a The syntax of the command is incorrect. error.


Where the error comes from

Let's say we wrote the above mentioned Batch Script to a file named make_init.cmd which lies in the same directory as the files init.marker, setup.py, setup.cfg. (We can create those files using the Batch Script see [1]. Check out this answer for more information on how to create empty files using a Batch Script.) Let's use cmd.exe to run it, which gives us following output:

User> make_init.cmd
User> FOR /F %i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%i
User> SET last_modified_file=init.marker
User> SET last_modified_file=setup.py
User> SET last_modified_file=setup.cfg
The syntax of the command is incorrect.
User> IF "setup.cfg"=="init.marker" (

Let's first see if our FOR statement works as intended by running the same script but swapping out the IF statement for a simple ECHO statement. (See [2])
The output is what we would expect:

User> make_init.cmd
User> COPY /B NUL  1>init.marker
User> COPY /B NUL  1>setup.py
User> COPY /B NUL  1>setup.cfg
User> FOR /F %i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%i
User> SET last_modified_file=init.marker
User> SET last_modified_file=setup.py
User> SET last_modified_file=setup.cfg
User> ECHO setup.cfg
setup.cfg

As far as I understand, this means that the mentioned command which is incorrect must be
IF "%last_modified_file%"=="init.marker" ( which evaluates to IF "setup.cfg"=="init.marker" (

How come then, that in any other circumstance (see [3]) such an IF statement is totally valid?


Question

What is wrong with the Syntax of the above IF command?


Attachments

[1] Script extension to create the needed files.

COPY /B NUL > init.marker
COPY /B NUL > setup.py
COPY /B NUL > setup.cfg
FOR /F %%i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%%i
IF "%last_modified_file%"=="init.marker"(
    ECHO Init has already been made
)

[2] Swap out the IF statement with an ECHO statement

COPY /B NUL > init.marker
COPY /B NUL > setup.py
COPY /B NUL > setup.cfg
FOR /F %%i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%%i
ECHO %last_modified_file%

[3] Simple test Script for an IF statement

:: Filename:        test_if.cmd
:: How to execute:  test_if.cmd hello

IF "%1"=="hello" (
    SET the_greeting_message=Greetings, traveller.
)
IF "%the_greeting_message%"=="Greetings, traveller." (
    ECHO %the_greeting_message%
)
Community
  • 1
  • 1
whme
  • 2,597
  • 3
  • 9
  • 20

1 Answers1

1

The problem is that you are missing a space before the trailing ( character on the if command line:

IF "%last_modified_file%"=="init.marker"(

Insert a space before the trailing ( character and it should parse correctly.

Bill_Stewart
  • 18,984
  • 4
  • 42
  • 53