0
set thisVer="2"
IF EXIST "C:\Program Files\Deployments\deploy.ver" (
    FOR /F "tokens=* USEBACKQ" %%F IN (`type "C:\Program Files\Deployments\deploy.ver"`) DO (
        type "C:\Program Files\Deployments\deploy.ver"
        set build=%%F
    )   
echo "Build version: %build%"
echo "This version: %thisVer%"
IF "%build%"=="%thisVer%" (
    echo "Same Versions"
    exit /B 0
)

The above code generates the following output...

"2"
"Build version:  Files\Deployments\deploy.ver"
"This version: "2""

Would someone please tell me why %build% is not equal to the contents of the file C:\Program Files\Deployments\deploy.ver?

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • [the Windows cmd is **not** DOS](https://superuser.com/q/451432/241386), there's no `for /f` command in DOS – phuclv Nov 06 '18 at 17:45
  • The duplicate I posted is only one of your problems. You are also missing a closing parentheses. You should never assign quotes to your variables directly. The `IF` command is a literal string comparison, so you have two sets of quotes on one side of the comparison and only one set on the left side. – Squashman Nov 06 '18 at 17:56
  • https://stackoverflow.com/questions/3068929/how-to-read-file-contents-into-a-variable-in-a-batch-file is where I grabbed the for /f from.... So I'm not sure why phuclv is referring to. – G. Malsack Nov 06 '18 at 18:03
  • 1
    @G.Malsack, you tagged your question with the `DOS` tag. You are not using `DOS`. You are using Windows. The `FOR /F` command was never a feature within `command.com` like it is with `cmd.exe`. – Squashman Nov 06 '18 at 18:07
  • https://stackoverflow.com/questions/30282784/variables-in-batch-not-behaving-as-expected - This resolved my problem. I added enabledelayedexpansion and replaced %build% with !build! – G. Malsack Nov 06 '18 at 18:09
  • @G.Malsack, as I stated in my previous comments, Delayed Expansion was only one of your problems. The `IF` comparison will fail as well because you are assigning quotes to the `thisVer` variable. – Squashman Nov 06 '18 at 18:10
  • AHHHHHHH, ok. sorry phuclv. Thank you for educating me on the difference between windows cmd and dos. Thank you Squashman for explaining that. – G. Malsack Nov 06 '18 at 18:11
  • hmmmm, the IF comparison seems to be working just as is though, since the file contains the quotes as well.... New output: "Build version: "2"" "This version: "2"" "Same Versions" – G. Malsack Nov 06 '18 at 18:12
  • @G.Malsack, ok that is fine but is not a best practice. I wouldn't store the value with quotes in the file, but you can strip the quotes when you assign the value to the build variable. `set build=%%~F` – Squashman Nov 06 '18 at 18:13
  • ok, I'll try to get that working as well. I really didn't like the quotes in the file anyway, however I was having issues with it prior to quoting the variable, so I just left it. Thanks for the heads up on this! :-) – G. Malsack Nov 06 '18 at 18:15

0 Answers0