1

I have a very simple Batch script that I'm working on

I'm considering implementing an updater with wget and version files locally and hosted on my server (yet I'm using Batch for some reason)

@echo off
for /f "tokens=* delims=" %%x in (version.txt) do set version=%%x 
if "%version%" == "1.0.0" (echo yes)

the text file it's referencing contains nothing but

1.0.0

Not even a new line. Echoing %version% gives me 1.0.0 in the console, but the if statement gets nothing.

Anyone wanna point out my error? I'm honestly clueless

Codor
  • 17,447
  • 9
  • 29
  • 56
StuAlex
  • 25
  • 4

2 Answers2

3

You appear to have a naughy space on the end of the set command.

Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"

The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • So, I think I understand what you're saying – StuAlex Jan 28 '15 at 16:10
  • Whoops, comments on newline – StuAlex Jan 28 '15 at 16:11
  • I changed the script a little `@echo off` `for /f "tokens=* delims=" %%x in ("%CD%\version.txt") do set /a curr=%%x` `if "%curr%" == "1.0.0" (echo yes)` And now it tells me Bad Parameter (Specifically when I added /a) – StuAlex Jan 28 '15 at 16:12
2

This is tricky. You have a space at the end of set version=%%x.

As a general point of scripting convention, whenever you set a variable to a string in a batch script, unless you have a specific reason not to do so, you should do set "variable=value" with the var=value pair quoted. That helps avoid problems like this. Your for /f line should end with set "version=%%x"

With the trailing space, you're basically saying if "1.0.0 " == "1.0.0", which is false.

As a side note, it is possible to fetch the content from your web server without using wget if you wish.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101