Page 1 of 1

Dos Batchfile Help

Posted: Wed Jun 14, 2006 4:58 pm
by Kon-Tiki
I'm trying to get a Dos batchfile to increment a variable, but so far, it keeps refusing.

This's the code:

Code: Select all

@echo off

SET count=01
GOTO LOOP

:ACTION
pkunzip CLSOTB%count%.ZIP
%count%+1
echo COUNT = %count%
GOTO :LOOP

:LOOP
IF EXIST CLSOTB%count%.ZIP GOTO ACTION
The line %count%+1 in ACTION is wrong. I've tried several other things, tried googling, but couldn't find anything that'd make it work. Does anybody here know?

Re:Dos Batchfile Help

Posted: Wed Jun 14, 2006 6:11 pm
by blip
AFAIK you can't do it in a simple way. I think you can use the utility CHOICE to do it but it's not pretty, requiring the /C option, redirection, errorlevels, IFs, and string appending. It gets nastier if your target doesn't support using %ERRORLEVEL% as a string. Windows XP doesn't come with the program but the version that comes with Windows 98 works on it fine. I'll see if I can come up with something definitive unless you decide you want to write a full program to do this instead.

Re:Dos Batchfile Help

Posted: Wed Jun 14, 2006 6:39 pm
by Kon-Tiki
Nah, just a batchfile's good. I've been hinted towards using a for-loop for this, and'm looking into that in the meantime.

Re:Dos Batchfile Help

Posted: Wed Jun 14, 2006 7:59 pm
by blip
You didn't specify the specific target OS which matters if you want to use FOR..LOOP. This is what I've got for systems that support using %ERRORLEVEL% as a string, untested:

Code: Select all

@echo off
set digit1=0
set digit0=1


:mainloop
if not exist CLSOTB%digit1%%digit0%.ZIP goto end

pkunzip CLSOTB%digit1%%digit0%.ZIP 


if not %digit0%==9 goto incrementdigit0
set digit0=0
goto incrementdigit1


:incrementdigit0
echo %digit0%>%TEMP%\t
choice/c:012345678/n<%TEMP%\t
set digit0=%ERRORLEVEL%
goto doneincrementing


:incrementdigit1
if %digit1%=9 goto end

echo %digit1%>%TEMP%\t
choice/c:012345678/n<%TEMP%\t
set digit1=%ERRORLEVEL%


:doneincrementing

echo COUNT = %digit1%%digit0%
goto mainloop

:end
del %TEMP%\t
set digit1=
set digit0=
It uses CHOICE to create a digit mapping that is consistent with incrementing, i.e. 0->1, 1->2,...,8->9. Before incrementing a digit it checks if it's 9 and if it is it makes it 0 and increments the second digit. There's no way to make a 9->0 mapping so you can't check for rollover after incrementing each digit and make it cleaner than the other way, the check must be done before it. For systems that don't support using %ERRORLEVEL% as a string you need to use an IF chain with descending values to make the appropriate digit variable equal to %ERRORLEVEL%.

Re:Dos Batchfile Help

Posted: Wed Jun 14, 2006 8:32 pm
by Kon-Tiki
I got this now:

Code: Select all

@echo off

FOR %%A IN (CLSOTB??.ZIP) DO pkunzip %%A 
Just tested it, and it works like a charm :)