gpt4 book ai didi

batch-file - 批处理 %errorlevel% 在 FOR 循环中返回 0

转载 作者:行者123 更新时间:2023-12-01 07:45:18 26 4
gpt4 key购买 nike

我正在尝试创建一个循环,该循环将遍历指定目录中的一组文本文件以搜索字符串。根据是否找到字符串来报告结果。但是%errorlevel%始终返回 0 并计算为 0。

SETLOCAL enabledelayedexpansion

FOR %%G IN (*.txt) DO (
find /i "My text string" "%%G"
ECHO %date% %time% : errorlevel is %errorlevel% >> %report_dir%\%computername%.txt
IF %errorlevel% EQU 1 (
ECHO %date% %time% : String found >> %report_dir%\%computername%.txt

GOTO:copy_log
)

)

ENDLOCAL

雷蒙德你是那个意思吗?:
SETLOCAL enabledelayedexpansion

FOR %%G IN (*.txt) DO (
find /i "My text string" "%%G"
IF %errorlevel% (
ECHO %date% %time% : String found >> %report_dir%\%computername%.txt

GOTO:copy_log
)

)

ENDLOCAL

最佳答案

%ERRORLEVEL%扩张太快了。您可以使用以下方法完全避免该问题:

IF ERRORLEVEL 1

或者有关更多详细信息,请参阅 SET /? 中对“延迟环境变量扩展”的解释。帮助文字:

Finally, support for delayed environment variable expansion has been added. This support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed. The following example demonstrates the problem with immediate variable expansion:

    set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)

would never display the message, since the %VAR% in BOTH IF statements is substituted when the first IF statement is read, since it logically includes the body of the IF, which is a compound statement. So the IF inside the compound statement is really comparing "before" with "after" which will never be equal. Similarly, the following example will not work as expected:

    set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%

in that it will NOT build up a list of files in the current directory, but instead will just set the LIST variable to the last file found. Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty. So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:

    set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)

Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty. So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:

    set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%

关于batch-file - 批处理 %errorlevel% 在 FOR 循环中返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139025/

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