DOS 7 中等效的 SET 命令
SET Command equivalent in DOS 7
我有一台旧的 MS DOS 计算机 运行 DOS 7.10(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 循环的方法。
for %%a in (1 2 3 4 5 6 7 8 9 10) do echo %%a
你应该从 1 数到 10。
除此之外,您需要在要求中更加明确。
在MS-DOS中,set
命令没有算术选项/A
。 for
循环也没有 /L
选项。
所以你要么必须这样做:
@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
此批处理文件执行您的请求。照原样,这个例子最多可以计数到 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
这是 MS-Dos/Win98 的 "general" 增量函数。
值的数字必须用逗号分隔。
结果将存储在同一个变量和用于打印的辅助变量中(不带逗号)。
此解决方案可以扩展以解决任何算术计算。
@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
你也可以把它当作一个"external"增量函数,你只需要将它命名为"increm.bat"
@echo off
set myCounterA=9,9
call increm.bat :inc myCounterA output
echo The new value is %output%
我有一台旧的 MS DOS 计算机 运行 DOS 7.10(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 循环的方法。
for %%a in (1 2 3 4 5 6 7 8 9 10) do echo %%a
你应该从 1 数到 10。
除此之外,您需要在要求中更加明确。
在MS-DOS中,set
命令没有算术选项/A
。 for
循环也没有 /L
选项。
所以你要么必须这样做:
@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
此批处理文件执行您的请求。照原样,这个例子最多可以计数到 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
这是 MS-Dos/Win98 的 "general" 增量函数。
值的数字必须用逗号分隔。
结果将存储在同一个变量和用于打印的辅助变量中(不带逗号)。
此解决方案可以扩展以解决任何算术计算。
@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
你也可以把它当作一个"external"增量函数,你只需要将它命名为"increm.bat"
@echo off
set myCounterA=9,9
call increm.bat :inc myCounterA output
echo The new value is %output%