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
I built my answer thanks to previous contributors.
Not having time for a custom counter.exe, I downloaded sed for FREEDOS.
And then the batch code could be, emulating "wc -l" with the utility sed, something like this according to your loop (I just use it to increment through executions starting from "1" to n+1):
Just remember to manually create a file "test.txt" with written on the first row
0
sed -n '$=' test.txt > counter.txt
set /P Var=< counter.txt
echo 0 >> test.txt
I realize you've found another answer - but the fact is that your original code was nearly correct but for a syntax error.
Your code contained the line
set /A COUNTER=%COUNTER%+1
and the syntax that would work is simply...
set /A COUNTER=COUNTER+1
See http://ss64.com/nt/set.html for all the details on the SET command. I just thought I'd add this clarification for anyone else who doesn't have the option of using FreeDOS.
Indeed, set
in DOS has no option to allow for arithmetic. You could do a giant lookup table, though:
if %COUNTER%==249 set COUNTER=250
...
if %COUNTER%==3 set COUNTER=4
if %COUNTER%==2 set COUNTER=3
if %COUNTER%==1 set COUNTER=2
if %COUNTER%==0 set COUNTER=1
None of these seemed to work for me:
@ECHO OFF
REM 1. Initialize our counter
SET /A "c=0"
REM Iterate through a dummy list.
REM Notice how the counter is used: "CALL ECHO %%c%%"
FOR /L %%i in (10,1,20) DO (
REM 2. Increment counter
SET /A "c+=1"
REM 3. Print our counter "%c%" and some dummy data "%%i"
CALL ECHO Line %%c%%: - Data: %%i
)
The answer was extracted from: https://www.tutorialspoint.com/batch_script/batch_script_arrays.htm (Section: Length of an Array)
Result:
Line 1: - Data: 10
Line 2: - Data: 11
Line 3: - Data: 12
Line 4: - Data: 13
Line 5: - Data: 14
Line 6: - Data: 15
Line 7: - Data: 16
Line 8: - Data: 17
Line 9: - Data: 18
Line 10: - Data: 19
Line 11: - Data: 20
A little bit late for the party, but it's an interessting question.
You can write your own inc.bat for incrementing a number.
It can increment numbers from 0 to 9998.
@echo off
if "%1"==":inc" goto :increment
call %0 :inc %counter0%
set counter0=%_cnt%
if %_overflow%==0 goto :exit
call %0 :inc %counter1%
set counter1=%_cnt%
if %_overflow%==0 goto :exit
call %0 :inc %counter2%
set counter2=%_cnt%
if %_overflow%==0 goto :exit
call %0 :inc %counter3%
set counter3=%_cnt%
goto :exit
:increment
set _overflow=0
set _cnt=%2
if "%_cnt%"=="" set _cnt=0
if %_cnt%==9 goto :overflow
if %_cnt%==8 set _cnt=9
if %_cnt%==7 set _cnt=8
if %_cnt%==6 set _cnt=7
if %_cnt%==5 set _cnt=6
if %_cnt%==4 set _cnt=5
if %_cnt%==3 set _cnt=4
if %_cnt%==2 set _cnt=3
if %_cnt%==1 set _cnt=2
if %_cnt%==0 set _cnt=1
goto :exit
:overflow
set _cnt=0
set _overflow=1
goto :exit
:exit
set count=%counter3%%counter2%%counter1%%counter0%
A sample for using it is here
@echo off
set counter0=0
set counter1=
set counter2=
set counter3=
:loop
call inc.bat
echo %count%
if not %count%==250 goto :loop
I've found my own solution.
Download FreeDOS from here: http://chtaube.eu/computers/freedos/bootable-usb/
Then using my Counter.exe file (which basically generates a Counter.txt file and increments the number inside every time it's being called), I can assign the value of the number to a variable using:
Set /P Variable =< Counter.txt
Then, I can check if it has run 250 cycles by doing:
if %variable%==250 echo PASS
BTW, I still can't use Set /A since FreeDOS doesn't support this command, but at least it supports the Set /P command.