2

我有一台运行 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 命令不起作用。不过,该命令在 Windows 10 中的 CMD 中有效。谁能建议我做错了什么?或者一种在 DOS 7 批处理文件中实现 for 循环的方法。

4

4 回答 4

3

/A在 MS-DOS 中,该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
于 2018-03-27T13:54:49.140 回答
3
for %%a in (1 2 3 4 5 6 7 8 9 10) do echo %%a

应该为你数 1 到 10。

除此之外,您需要更明确地提出您的要求。

于 2018-03-27T13:42:30.393 回答
3

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

编辑 2018 年 3 月 27 日按照评论中的建议修改代码

编辑 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
于 2018-03-27T21:09:26.713 回答
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%
于 2018-03-28T15:08:14.710 回答