How to increment variable under DOS?

前端 未结 9 1658
清酒与你
清酒与你 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:17

    Directly from the command line:

     for /L %n in (1,1,100) do @echo %n
    

    Using a batch file:

     @echo off
     for /L %%n in (1,1,100) do echo %%n
    

    Displays:

     1
     2
     3
     ...
     100
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-02-06 22:24

    Coming to the party very very late, but from my old memory of DOS batch files, you can keep adding a character to the string each loop then look for a string of that many of that character. for 250 iterations, you either have a very long "cycles" string, or you have one loop inside using one set of variables counting to 10, then another loop outside that uses another set of variable counting to 25.

    Here is the basic loop to 30:

    @echo off
    rem put how many dots you want to loop
    set cycles=..............................
    set cntr=
    :LOOP
    set cntr=%cntr%.
    echo around we go again
    if "%cycles%"=="%cntr%" goto done
    goto loop
    :DONE
    echo around we went
    
    0 讨论(0)
提交回复
热议问题