gpt4 book ai didi

batch-file - 如何在批处理脚本中使用 goto

转载 作者:行者123 更新时间:2023-12-04 01:50:51 25 4
gpt4 key购买 nike

我写了以下代码

setlocal

set /A sample =1

:first

type C:\test.txt | find "inserted"

if %ERRORLEVEL% EQU 0 goto test

if %ERRORLEVEL% EQU 1 goto exam

:test

echo "testloop" >> C:\testloop.txt

set /A sample = %sample% + 1

if %sample% LEQ 4 goto first

:exam

echo "exam loop" >> C:\examloop.txt

endlocal

但即使错误级别不等于“1”,它也会标记“考试”,请帮助我

最佳答案

你的问题不是 goto,它的 errorlevel 需要特殊处理,它不像一个普通的环境变量。您可以对 errorlevel 进行的唯一测试是测试它是否大于或等于 value。

所以你必须从最高到最低测试 errorlevel 值,因为如果 errorlevel 1
然后 if errorlevel 1会是真的,但是 if errorlevel 0也将是真的

setlocal
set /A sample =1

:first
type C:\test.txt | find "inserted"

if errorlevel 1 goto exam
if errorlevel 0 goto test

:test
echo "testloop" >> C:\testloop.txt
set /A sample = %sample% + 1

if %sample% LEQ 4 goto first

:exam
echo "exam loop" >> C:\examloop.txt

endlocal

如果您启用了命令扩展,并且没有名为 ERRORLEVEL(不区分大小写)的环境变量。那么理论上你可以像普通环境变量一样使用 %ERRORLEVEL% 。所以这也应该有效
setlocal EnableExtensions
set /A sample =1

:first
type C:\test.txt | find "inserted"

if %errorlevel% EQU 1 goto exam
if %errorlevel% EQU 0 goto test

:test
echo "testloop" >> C:\testloop.txt
set /A sample = %sample% + 1

if %sample% LEQ 4 goto first

:exam
echo "exam loop" >> C:\examloop.txt

关于batch-file - 如何在批处理脚本中使用 goto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2197945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com