0
set month = 10
for /f "tokens=*" %%i in (input.txt) do (
    if /I not %%i == %month% (
        echo %%i >> "output.txt"
    )
)

I got '10' is not recognized as an internal or external command, operable program or batch file. Can anyone tell me why is this happening and how can I compare "10"?

phuclv
  • 37,963
  • 15
  • 156
  • 475
simple guy
  • 633
  • 1
  • 6
  • 19
  • Possible duplicate of [Declaring and using a variable in Windows batch file (.BAT)](https://stackoverflow.com/questions/10552812/declaring-and-using-a-variable-in-windows-batch-file-bat) – phuclv Jul 04 '18 at 05:45

1 Answers1

3

Spaces in set command is significant. set month = 10 will create a variable named "month " (with space after) with a value " 10" (with a space before). Just try echo %month % and see

Because the variable %month% is not available, if /I not %%i == %month% ( will be expanded as if /I not %%i == (, which results in an invalid syntax. Another lesson: always surround if parameters with "" so if the variable expands to an empty string it'll still work

Besides, to avoid a trailing space is included on each line, you should move the redirection to before the command

The final result will be like this

set month=10
for /f "tokens=*" %%i in (input.txt) do (
    if /I not "%%i" == "%month%" (
        >>"output.txt" echo %%i
    )
)
phuclv
  • 37,963
  • 15
  • 156
  • 475