0
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 :).

1
  • Note that you shouldn't call exit in a batch file unless your intention is actually to exit the command processor, e.g. in an interactive session as well. To exit a batch file use exit /b or goto :eof. Commented Feb 14, 2013 at 17:24

1 Answer 1

1

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 ;)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.