Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment variable under DOS?

Tags:

batch-file

dos

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:\counter.txt if %COUNTER% EQU %Cycles% goto Pass if NOT %COUNTER% EQU %Cycles% goto Cycle  :START Set COUNTER=0 set CYCLES=250  :Cycle set /A COUNTER=%COUNTER%+1     <----------- PROBLEM echo Iteration %COUNTER% of %CYCLES% echo Delay 3 seconds to reboot choice /cy /n /t:y,3 warmboot.com  :Pass pause 

What it's doing is it runs the file "warmboot.com" (which reboots my PC) and runs for 250 cycles. Once the cycles have run 250 times (i.e when counter equals cycles) then it stops.

Under Windows, this works. However, this doesn't work under DOS environment. I've tried versions from v4 all the way up to v6.22 and even v7 and they all fail when it reaches the "PROBLEM" line.

If I do this:

set /A COUNTER=%COUNTER%+1 echo %Counter% 

OR this:

set /A COUNTER+=1 echo %Counter% 

both will return a blank line, i.e it shows nothing as the output.

If I type:

set /? 

then it shows this:

Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.

but typing the same command under CMD in Windows shows a lot more stuff. I'm thinking that the SET function under DOS doesn't support arithmetic functions, but for work purposes I have to run my scripts in DOS only.

Any ideas?

like image 382
shadowz1337 Avatar asked Feb 11 '14 08:02

shadowz1337


People also ask

How do you set a variable increment?

Used to increase the value of a "counter" variable. Normally, counters are used inside of a loop. In the Available Actions pane, open the Variables folder and double-click the Increment Variable action or drag it into the Steps pane. In the Variable Name box, select the variable to increment.

What does %% mean in DOS?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do you set a variable in DOS?

Syntax SET variable SET variable=string SET "variable=string" SET "variable=" SET /A "variable=expression" SET /P variable=[promptString] SET " Key variable : A new or existing environment variable name e.g. _num string : A text string to assign to the variable.

How do you set a variable in CMD?

2.2 Set/Unset/Change an Environment Variable for the "Current" CMD Session. To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.


Video Answer


2 Answers

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.

like image 144
Furr Avatar answered Sep 28 '22 11:09

Furr


Indeed, set in that old DOS (rather than Windows' cmd) has no option to allow for arithmetic. (cmd does have the way you described.) 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 
like image 42
Joey Avatar answered Sep 28 '22 11:09

Joey