DOS 7中的SET命令等效

时间:2018-03-27 13:19:07

标签: batch-file dos windows-98

我有一台运行DOS 7.10的旧MS DOS计算机(ver命令给出:windows 98 ver 4.10.2222)。我必须创建一个基本上运行命令10或任何时候的批处理脚本。我尝试使用for命令,但它给了我ILLEGAL Command For 所以现在我有:

@ECHO off
SET COUNT=0

:MyLoop
IF "%COUNT%" == "10" GOTO EndLoop
ECHO %COUNT%
SET /a COUNT+=1
:EndLoop 
ECHO done

然而,这给了我一个0的无限循环,好像set命令不起作用。虽然命令DOES在Windows 10中的CMD中工作。任何人都可以建议我做错了什么?或者在DOS 7批处理文件中实现for循环的方法。

4 个答案:

答案 0 :(得分:3)

for %%a in (1 2 3 4 5 6 7 8 9 10) do echo %%a

对你来说应该是1到10个。

除此之外,您需要更明确地满足您的要求。

答案 1 :(得分:3)

在MS-DOS中,/A命令没有算术选项set/L循环也没有for选项。

所以你要么必须这样做:

@echo off
set COUNT=

:LOOP
if "%COUNT%"=="__________" goto QUIT
set COUNT=%COUNT%_
echo %COUNT%
goto LOOP
:QUIT
echo Done

或者像这样:

@echo off
for %%I in (1 2 3 4 5 6 7 8 9 10) do echo %%I
echo Done

如果你想做很多次迭代,你可以帮助自己这样做第一种方法:

@echo off
rem // This performs 1000 iterations:
set COUNT=
set LIMIT=__________
set LIMIT=%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%
set LIMIT=%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%%LIMIT%

:LOOP
if "%COUNT%"=="%LIMIT%" goto QUIT
set COUNT=%COUNT%_
echo %COUNT%
goto LOOP
:QUIT
echo Done

或者像第二个这样:

@echo off
rem // This performs 1000 iterations:
for %%I in (1 2 3 4 5 6 7 8 9 10) do for %%J in (1 2 3 4 5 6 7 8 9 10) do for %%K in (1 2 3 4 5 6 7 8 9 10) do echo %%I, %%J, %%K
echo Done

答案 2 :(得分:3)

此批处理文件执行您请求的操作。原样,此示例最多可计数123,但最多可计数999(按您的要求为1000次)。如果您需要更多数字,只需添加相应的部分......

编辑03/27/2018 修改后的代码

编辑03/29/2018 第二次尝试

@echo off
if not "%1" == "" goto %1

set myself=%0

set count1=0

:MyLoop
   if "%count3%%count2%%count1%" == "123" goto EndLoop
   echo %count3%%count2%%count1%
   call %myself% :incCount
goto MyLoop
:endLoop
echo Done
goto :EOF


:incCount
call %myself% :incDigit %count1%
set count1=%digit%
if %carry% == 0 goto endIncCount
call %myself% :incDigit %count2%
set count2=%digit%
if %carry% == 0 goto endIncCount
call %myself% :incDigit %count3%
set count3=%digit%
:endIncCount
goto :EOF

:incDigit digit
set carry=0
if not "%2" == "" goto next1
   set digit=1
   goto endIncDigit
:next1
if not %2 == 9 goto next2
   set digit=0
   set carry=1
   goto endIncDigit
:next2
if %2 == 8 set digit=9
if %2 == 7 set digit=8
if %2 == 6 set digit=7
if %2 == 5 set digit=6
if %2 == 4 set digit=5
if %2 == 3 set digit=4
if %2 == 2 set digit=3
if %2 == 1 set digit=2
if %2 == 0 set digit=1
:endIncDigit

:EOF

编辑添加新方法

这种更简单的方法可以管理计数器中的任意数量的数字而无需修改:

@echo off
if not "%1" == "" goto %1

set myself=%0

set count=0

:MyLoop
   call %myself% :incCount 
   echo %printCnt%
if not "%printCnt%" == "123" goto MyLoop
echo Done
goto :EOF


:incCount
set newCnt=
set printCnt=
set carry=1
for %%a in (%count%) do call %myself% :incDigit %%a
set count=%newCnt%
if %carry% == 0 goto :EOF
set count=%count%,1
set printCnt=1%printCnt%
goto :EOF

:incDigit digit
set digit=%2
if %carry% == 0 goto endIncDigit
if not %2 == 9 goto next
   set digit=0
   goto endIncDigit
:next
if %2 == 8 set digit=9
if %2 == 7 set digit=8
if %2 == 6 set digit=7
if %2 == 5 set digit=6
if %2 == 4 set digit=5
if %2 == 3 set digit=4
if %2 == 2 set digit=3
if %2 == 1 set digit=2
if %2 == 0 set digit=1
set carry=0
:endIncDigit
set newCnt=%newCnt%,%digit%
set printCnt=%digit%%printCnt%

:EOF

答案 3 :(得分:1)

这是MS-Dos / Win98的“常规”增量功能。 值的数字必须用逗号分隔。 结果将存储在同一变量和二级变量中以进行打印(不带逗号) 可以扩展此解决方案以解决任何算术计算。

@echo off
if not "%1" == "" goto %1

set counter=0

:loop
call %0 :inc counter print_cnt
echo counter=%print_cnt%
goto :loop
goto :eof

REM *************************************
:inc
set _self=%0
set _counterVar=%2
set _counterPrintVar=%3
set _rev=
set _result=
set _printResult=
set _carry=1
call %_self% :GetIndirectVar _counter %_counterVar%
call %_self% :reverse %_counter%
set %_counterVar%=%_result%
set %_counterPrintVar%=%_printResult%
goto :eof

REM *************************************
:GetIndirectVar
for %%a in (%3) do echo set %2=%%%%a%% > tmp.bat
call tmp.bat
del tmp.bat
goto :eof

REM *************************************
:reverse
if "%2" == "" goto :_add_start
set _rev=%2,%_rev%
shift
goto :reverse

:_add_start
for %%a in (%_rev%) do call %_self% :add_digit %%a
if "%_carry%" == "0" goto :eof
set _digit=1
goto :_add_digit_end

REM *************************************
:add_digit
set _digit=%2
rem echo d=%2 carry=%_carry%
if "%_carry%" == "0" goto :_add_digit_end
set _carry=0

if "%_digit%" == "9" set _carry=1
if "%_digit%" == "9" set _digit=0
if "%_carry%" == "1" goto :_add_digit_end
if "%_digit%" == "8" set _digit=9
if "%_digit%" == "7" set _digit=8
if "%_digit%" == "6" set _digit=7
if "%_digit%" == "5" set _digit=6
if "%_digit%" == "4" set _digit=5
if "%_digit%" == "3" set _digit=4
if "%_digit%" == "2" set _digit=3
if "%_digit%" == "1" set _digit=2
if "%_digit%" == "0" set _digit=1

:_add_digit_end
set _result=%_digit%,%_result%
set _printResult=%_digit%%_printResult%
goto :eof

:eof

您也可以将它用作“外部”增量功能,您只需将其命名为“increm.bat”

@echo off
set myCounterA=9,9
call increm.bat :inc myCounterA output
echo The new value is %output%