if 语句第一次有效,但第二次无效

发布于 2024-12-22 19:15:19 字数 1346 浏览 1 评论 0原文

我遇到了这个问题 ** 如果未处理 == %true% **
问题是它在不应该的情况下停留在循环中。
我尝试了另一种方法 IFprocessed==%false% 它进入循环但不会在第二次返回。有时,最多会出现 3 次。

我的“echo !processed! process”总是给我正确的数字,但 IF 语句只是不第二次处理

%TRUE% 和 %FALSE% 是全局变量 1 和 0

   setlocal  
    :loopapp  
    **if not '!processed!'=='%TRUE%'** (  
        set /a count+=1
        "%ProgramFiles%\abc\abc.exe" !file! !post!  
        call :ERRORCODES !file! !post! !ERRORLEVEL! !count!  
        goto :loopapp  
    )
    endlocal

:ERRORCODES
setlocal
if %errornum% LEQ 99 (  
    set no_license=%FALSE%  
    if '!post!'== 'A' set no_license=%TRUE%  

    if '!no_license!'=='%TRUE%' (
            echo    Searching ... '!count!' ... Please Wait ...
            if !count! EQU 5 (
                set execute=%TRUE%
                set succes=%FALSE%
                echo %~n1 - Fatal - process %TIME% >> %tempfolder%\errorlog.txt
                goto :END
            )  
            set execute=%FALSE%  
            set succes=%FALSE%  
            goto :out_errorcodes  
    )
    set execute=%TRUE%
    set succes=%FALSE%
    echo %~n1 - Fatal - process %TIME% >> %tempfolder%\errorlog.txt  
    goto :out_errorcodes 
)  

... other errors  

:out_errorcodes  
endlocal & set processed=%execute% & set fait=%succes%  
goto :EOF

I'm having this problem with the ** IF NOT processed == %true% **.
The problem is that it's staying in the loop when it shouldnt.
I tried the otherway around IF processed == %false% it goes in the loop BUT doesnt go back in the 2nd time. sometimes, it goes in up to 3 times.

my "echo !processed! process" is always giving me the right number but the IF statement is just not processing the second time around

%TRUE% AND %FALSE% are global variable 1 and 0

   setlocal  
    :loopapp  
    **if not '!processed!'=='%TRUE%'** (  
        set /a count+=1
        "%ProgramFiles%\abc\abc.exe" !file! !post!  
        call :ERRORCODES !file! !post! !ERRORLEVEL! !count!  
        goto :loopapp  
    )
    endlocal

:ERRORCODES
setlocal
if %errornum% LEQ 99 (  
    set no_license=%FALSE%  
    if '!post!'== 'A' set no_license=%TRUE%  

    if '!no_license!'=='%TRUE%' (
            echo    Searching ... '!count!' ... Please Wait ...
            if !count! EQU 5 (
                set execute=%TRUE%
                set succes=%FALSE%
                echo %~n1 - Fatal - process %TIME% >> %tempfolder%\errorlog.txt
                goto :END
            )  
            set execute=%FALSE%  
            set succes=%FALSE%  
            goto :out_errorcodes  
    )
    set execute=%TRUE%
    set succes=%FALSE%
    echo %~n1 - Fatal - process %TIME% >> %tempfolder%\errorlog.txt  
    goto :out_errorcodes 
)  

... other errors  

:out_errorcodes  
endlocal & set processed=%execute% & set fait=%succes%  
goto :EOF

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-12-29 19:15:19

1) 我不明白您在哪里定义 TRUEFALSE 是什么 - 它们当然不是标准批次值。

如果您尝试设置充当布尔标志的变量,那么我建议您执行以下操作。

要将标志设置为 false,请使用 SET "FLAG="。这“未定义”标志。

要将标志设置为 true,请使用 SET "FLAG=1。请注意,值 1 没有意义。只要不为空,您可以使用任何值。

测试标志是否为 true使用 IF DEFINED FLAG ECHO FLAG IS TRUE

来测试该标志是否为 false 使用 IF NOT DEFINED FLAG ECHO FLAG IS FALSE

我喜欢这种技术的原因是对 DEFINED 进行评估 中使用此测试都是安全的,并且您不必担心延迟扩展。

在执行时,而不是在解析时,因此在任何代码块(例如 FOR 循环内) 没有费心去尝试跟踪你的逻辑。如果你能拿出最少的代码来演示这个问题,那就太好了

3) 如果你在其他地方使用过 ECHO OFF,请尝试设置。 ECHO ON 位于 IF 之前,以便您可以看到批处理脚本的内容正在尝试执行此操作

(我以为您可能缺少 SETLOCAL EnableDelayedExpansion,但现在我看到您的报告显示 echo !processed! process 有效。)

1) I don't see where you define what TRUE and FALSE are - they are certainly not standard batch values.

If you are trying to set up variables that function as boolean flags, then I recommend the following.

To set the flag to false, use SET "FLAG=". This "undefines" the flag.

To set the flag to true, use SET "FLAG=1. Note that the value 1 has no significance. You could use any value as long as it is not empty.

To test if the flag is true use IF DEFINED FLAG ECHO FLAG IS TRUE

To test if the flag is false use IF NOT DEFINED FLAG ECHO FLAG IS FALSE

The reason I like this technique is that DEFINED is evaluated at execution time, not parse time. So it is safe to use this test within any block of code such as within a FOR loop, and you don't have to worry about delayed expansion.

2) I haven't bothered to try to trace your logic. It would be good if you can come up with a minimal amount of code that demonstrates the problem.

3) If you have used ECHO OFF elsewhere, try setting ECHO ON just prior to the IF so that you can see what the batch script is attempting to do.

(I thought perhaps you were missing SETLOCAL EnableDelayedExpansion, but now I see your report that echo !processed! process works.)

遮了一弯 2024-12-29 19:15:19

您的子程序似乎不起作用的原因是您在其中犯了一个小错误。

endlocal & set processed=%execute% & set fait=%succes%

%execute% 和 & 之间有一个空格。
该空间也将包含在 %processed% 变量的值中。
因此,在您的 if 语句中,这将是 true

'!processed!'=='%TRUE% '

为了避免在 %processed% 变量中出现该空间,请使用此

endlocal & set processed=%execute%& set fait=%succes%

这也记录在 SS64

The reason your subprogram does not seem to work is because you made a little error in it.

endlocal & set processed=%execute% & set fait=%succes%

You have a space between %execute% and &.
That space will also be in the %processed% variable's value.
So in your if statement this will be true

'!processed!'=='%TRUE% '

To avoid having that space in the %processed% variable use this

endlocal & set processed=%execute%& set fait=%succes%

This is also documented on SS64

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文