How to increment variable under DOS?

前端 未结 9 1763
清酒与你
清酒与你 2021-02-06 21:36

I\'ve spent the past 3hrs trying to work this out but just couldn\'t find a solution. Here\'s my batch script:

if NOT Exist Counter.txt GOTO START
Type c:\\count         


        
9条回答
  •  故里飘歌
    2021-02-06 22:20

    I didn't use DOS for - puh - feels like decades, but based on an old answer and my memories, the following should work (although I got no feedback, the answer was accepted, so it seems to work):

    @echo off
      REM init.txt should already exist
      REM to create it:
      REM   COPY CON INIT.TXT
      REM   SET VARIABLE=^Z
      REM ( press Ctrl-Z to generate ^Z )
      REM
      REM also the file "temp.txt" should exist.
    REM add another "x" to a file:
    echo x>>count.txt
    REM count the lines in the file and put it in a tempfile:
    type count.txt|find /v /c "" >temp.txt
    
    REM join init.txt and temp.txt to varset.bat:
    copy init.txt+temp.txt varset.bat
    REM execute it to set %variable%:
    call varset.bat
    
    for %%i in (%variable%) do set numb=%%i
    echo Count is: %numb%
       REM just because I'm curious, does the following work? :
       set numb2=%variable%
       echo numb2 is now %var2%
    if %numb%==250 goto :finished
    echo another boot...
    warmboot.exe
    :finished
    echo that was the last one.
    

    In DOS, neither set /a nor set /p exist, so we have to work around that. I think both for %%i in (%variable%) do set numb=%%i and set numb2=%variable% will work, but I can't verify.

    WARNING: as there is no ">" or "<" comparison in DOS, you should delete the batchfile at the :finished label (because it continues to increment and 251 is not equal 250 anymore)

    (PS: the basic idea is from here. Thanks foxidrive. I knew, I knew it from StackOverflow but had a hard time to find it again)

提交回复
热议问题