Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批量模拟while循环_Batch File_Cmd - Fatal编程技术网

Batch file 批量模拟while循环

Batch file 批量模拟while循环,batch-file,cmd,Batch File,Cmd,我正在尝试成批模拟while循环,下面是我的代码: @echo off set test=0 :main call:whileLSS %test% 4 :count :whileLSS if %1 LSS %2 ( echo %1 call%3 goto whileLSS ) goto:EOF :count set /a test=%test%+1 goto:EOF 这只是输出0,而不是像我希望的那样输出“01 2 3” 问题是循环将永远运行,因为%1没有最新的t

我正在尝试成批模拟while循环,下面是我的代码:

@echo off
set test=0

:main
call:whileLSS %test% 4 :count

:whileLSS
if %1 LSS %2 (
    echo %1
    call%3
    goto whileLSS
)
goto:EOF

:count
set /a test=%test%+1
goto:EOF
这只是输出0,而不是像我希望的那样输出“01 2 3”

问题是循环将永远运行,因为%1没有最新的test值

这是正确的方法吗

如何更新%1的值

有没有办法不必硬编码像LSS这样的运算符?

像这样的可能

@echo off

:main
set /a a=1
set /P i=Enter i:
call:whileLSS %a% %i%

:whileLSS
    echo %1
    if %1 LSS %2  call:reinitialize %1 %2
    goto:EOF


:reinitialize
    set /a c=%1
    set /a b=%c%+1
    set /a d=%2
    call:whileLSS %b% %d%

goto:EOF

正如您所知,您不能更改Arg,您可以将Arg作为引用并更改引用的var,这需要在此处延迟扩展。
你的第一艘潜艇也没有与flow分离

这批:

@echo off&Setlocal EnableDelayedExpansion
set test=0

:main
call:whileLSS test 4 :count
Goto :Eof

:whileLSS
if !%1! LSS %2 (
    echo !%1!
    call%3
    goto whileLSS
)
goto:EOF

:count
set /a test+=1
goto:EOF
生成此输出:

0
1
2
3
编辑
if的操作员也可以作为arg提供:

@echo off&Setlocal EnableDelayedExpansion
set test=0

:main
call:while test LSS 4 :Increment
set test=10
call:while test GTR 4 :Decrement

Goto :Eof
:while
if !%1! %2 %3 (
    echo !%1!
    call %4 %1
    goto while
)
goto:EOF

:Increment
set /a %1+=1
goto:EOF

:Decrement
set /a %1-=1
goto:EOF
试试这个:

@echo off 
Setlocal EnableDelayedExpansion
set test=0

:main
call :whileLSS !test! 4 
Goto :Eof

:whileLSS
set i=%1
set j=%2

:loop
if !i! LSS !j! (
    echo !i!
    call :count
    goto :loop
)

goto :EOF

:count
set /a i+=1
goto :EOF

看看这个。无法更新命令行参数的值。您必须首先将其分配给一个环境变量,然后再对其进行更新。相关:即使这似乎可以回答问题,最好对您的代码和操作代码进行解释(为什么它不起作用,为什么您的代码执行所需的操作)是否有方法使用另一个参数来使用运算符?这样我就不必为LEQ、GTR和GEQ@Nihil是的,刚刚测试过,请参见答案的附件。@Nihil对subs进行了另一次编辑,但我现在感觉有点虚拟化;-)我个人更喜欢一年后仍能理解的直接代码。奇怪的是,出于某种原因,当我尝试使用第四个参数时,它的值总是“4”,现在它可以工作了:)