无法比较批处理文件中的2个整数变量(Unable to compare 2 integer variables in batch file)

编程入门 行业动态 更新时间:2024-10-11 13:19:16
无法比较批处理文件中的2个整数变量(Unable to compare 2 integer variables in batch file)

我有以下这段代码,我基本上试图比较批处理文件中的2个整数(其中一个从注册表中检索) -

set mainCounter=1 reg add HKLM\Software\Looptest /f /v mainCounter1 /t REG_SZ /d 0 rem ---- some code here --- FOR /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Looptest" /v "mainCounter1"') do set "mainCounter1=%%b" echo mainCounter :: %mainCounter% echo mainCounter1 :: %mainCounter1% if %mainCounter% EQU %mainCounter1% goto _reImageSystem :_reImageSystem echo mainCounter while reimaging :: %mainCounter% echo mainCounter1 while reimaging :: %mainCounter1% pause

这段代码清楚地表明,mainCounter的值是1(手动设置)而且,mainCounter1的值设置为registry ..这是0.但是,当我比较if %mainCounter% EQU %mainCounter1% goto _reImageSystem ,显然应该不要进入_reImageSystem,但它会......

输出是:

mainCounter :: 1 mainCounter1 :: 0 mainCounter while reimaging :: 1 mainCounter1 while reimaging :: 0

所以,可能比较不起作用..

if %mainCounter% EQU %mainCounter1% goto _reImageSystem

有什么建议么 ?

谢谢!

I have this following piece of code where I am basically trying to compare 2 integers in a batch file (one of them is retrieved from registry) -

set mainCounter=1 reg add HKLM\Software\Looptest /f /v mainCounter1 /t REG_SZ /d 0 rem ---- some code here --- FOR /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Looptest" /v "mainCounter1"') do set "mainCounter1=%%b" echo mainCounter :: %mainCounter% echo mainCounter1 :: %mainCounter1% if %mainCounter% EQU %mainCounter1% goto _reImageSystem :_reImageSystem echo mainCounter while reimaging :: %mainCounter% echo mainCounter1 while reimaging :: %mainCounter1% pause

This code clearly suggests that, value of mainCounter is 1 (set manually) And, The value of mainCounter1 is set to registry.. which is 0. Still , when I compare if %mainCounter% EQU %mainCounter1% goto _reImageSystem , clearly it should not go inside _reImageSystem, but it goes...

The output is:

mainCounter :: 1 mainCounter1 :: 0 mainCounter while reimaging :: 1 mainCounter1 while reimaging :: 0

So, possible the comparison is not working..

if %mainCounter% EQU %mainCounter1% goto _reImageSystem

Any suggestions ?

Thanks!

最满意答案

如果比较为假,它仍将落入标签并执行。

为了防止这个分支:

if %mainCounter% EQU %mainCounter1% goto _reImageSystem goto end :_reImageSystem bla bla bla bla :end

(或GOTO:eof结束或反转你的逻辑并使用NEQ )

If the comparison is false it will still fall through to the label and execute.

To prevent this branch away:

if %mainCounter% EQU %mainCounter1% goto _reImageSystem goto end :_reImageSystem bla bla bla bla :end

(Or GOTO:eof to end or invert your logic and use NEQ)

更多推荐

本文发布于:2023-08-01 02:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   变量   批处理文件   Unable   compare

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!