prom
prom

Reputation: 69

cmd batch redirecting (appending) to file given by variable

For some unknown reason the following code in my batch script

:subroutine    
IF /I %firstPass%==true head -n 1 "%1%" >> "%exFilename%"

renders as

IF /I true == true head -n 1 ""request_sql.csv"exFilename"

The problem is that redirection >> is drops out and the command stumbles. Using plain string for identification of the file works ok. Would be thankful for help.

Upvotes: 0

Views: 45

Answers (1)

MC ND
MC ND

Reputation: 70923

                                   first argument = "request_sql.csv"
                                   vv
IF /I %firstPass%==true head -n 1 "%1%" >> "%exFilename%"
                                     ^......^
                                      undefined variable

And you get

IF /I true == true head -n 1 ""request_sql.csv"exFilename"

As the correct way to reference the first argument is %1, without closing percent sign, you should use

IF /I "%firstPass%"=="true" head -n 1 "%~1" >> "%exFilename%"

where %~1 is the content of the first argument without quotes, as you are including them in the command

Upvotes: 3

Related Questions