user2072977
user2072977

Reputation: 3

Batch script comparison issue

if exist "C:\Windows\System32\updatevmcheck.txt" (
set /p Build=<C:\Windows\System32\updatevm.txt
if %Build% LSS 2  (
echo "Run Code Here"
) else (
exit
)
echo 2 > C:\Windows\System32\updatevmcheck.txt
exit
) 
ELSE (
echo 1 > C:\Windows\System32\updatevmcheck.txt
exit
)

The above is the current code I have. Basically it checks for the existence of a file, if it's there it checks the file to see if the number in it is less than the one you specify. If so, it runs some code, then updates the number in the file then exits. Otherwise, it creates the file with a number then exits. I believe my syntax is correct and I can run the individual lines, however when I make my batch file it doesn't even seem to get past the if exist statement. Can anyone see anything blatantly wrong with this besides the poor formatting :).

Upvotes: 0

Views: 185

Answers (1)

Joey
Joey

Reputation: 354406

You're setting a variable within a block and using its value in the same block. This cannot work with normal variable expansion (which happens as a command (this includes a complete block) is parsed, not when it's run). To solve this you need to use delayed expansion, so put the following at the start of your batch file:

setlocal enabledelayedexpansion

and then use !Build! instead of %Build%. See help set for more details and an explanation (I've written it a few dozen times by now ;)).

Upvotes: 1

Related Questions