开发者

What is wrong with this batch script?

开发者 https://www.devze.com 2023-02-28 20:29 出处:网络
I need a batch that reads a number from a file, increments it and saves it back into this file... This is what I came up with:

I need a batch that reads a number from a file, increments it and saves it back into this file... This is what I came up with:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

IF EXIST script\BUILDVERSION (
  SET /p input = <script\BUILDVERSION
  SET /a result=%input%+1
  ECHO %result% > script\BUILDVERSION
) ELSE (
  ECHO 0 > script\BUILDVERSION
)

At first it worked in a strange way, the result from reading the number from the file seemed to be a small random number, the result of the sum seemed random too... I don't know what I did, but now开发者_运维百科 it doesn't even read the number from file into the variable...

Thanks in advance for help!


Instead of %input% and %result%, try using !input! and !result!. This seems to work better when using delayed expansion. Also, make sure you don't have any unnecessary spaces when reading from the file. You'll end up with:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

IF EXIST script\BUILDVERSION (
  SET /p input=<script\BUILDVERSION
  SET /a result=!input!+1
  ECHO !result! > script\BUILDVERSION
) ELSE (
  ECHO 0 > script\BUILDVERSION
)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号