在 DOS 批处理中比较 2 个数字不起作用

发布于 2024-12-10 09:17:56 字数 642 浏览 0 评论 0原文

我是 DOS 批处理编程的老手,也是新手。我有一个认为非常简单的批处理脚本,但无法工作。我搜索了类似的帖子,但没有找到匹配的帖子。

我在 XP 上运行以下脚本。我的目标是在继续操作之前检查可用磁盘空间,但我在比较 2 个数字时遇到了问题,因此下面的脚本仅包含该逻辑。我有硬编码的数字来显示问题,即...比较(if x gtr y)似乎不起作用,因此分支逻辑到了错误的地方。要么是这样,要么我在 IF 语句的其他地方搞砸了。 (有些 echo 语句是不必要的 - 它们用于调试 - 但我现在将它们保留在其中。)

对于我出错的地方的任何启发将不胜感激。

谢谢...

@echo off

set Free=217522712576
set Need=20000000000

echo Free=%Free%
echo Need=%Need%

echo on
IF %Free% GTR %Need% (GOTO Sufficient_Space) ELSE GOTO Insufficient_Space
@echo off

:Insufficient_Space
@ECHO INSUFFICIENT SPACE
GOTO DONE

:Sufficient_Space
@ECHO SUFFICIENT SPACE

:DONE

I'm an old-timer who is a newbie to DOS Batch programming. I have what I think is a very simple batch script, that is not working. I looked for similar posts, and didn't find one that matched.

I am running the below script on XP. My goal is to check for free disk space before proceeding further, but I ran into a problem comparing 2 numbers, so the below script contains only that logic. I have hard-coded numbers to show the problem, which is... The comparison (if x gtr y) seems not to work, and so the branch logic goes to the wrong place. Either that, or I'm messing up somewhere else in the IF statement. (Some of the echo statements are unnecessary - they are for debugging - but I left them in for now.)

Any enlightenment on where I'm going wrong would be GREATLY appreciated.

Thx...

@echo off

set Free=217522712576
set Need=20000000000

echo Free=%Free%
echo Need=%Need%

echo on
IF %Free% GTR %Need% (GOTO Sufficient_Space) ELSE GOTO Insufficient_Space
@echo off

:Insufficient_Space
@ECHO INSUFFICIENT SPACE
GOTO DONE

:Sufficient_Space
@ECHO SUFFICIENT SPACE

:DONE

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

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

发布评论

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

评论(3

长梦不多时 2024-12-17 09:17:56

这些数字会溢出 32 位整数,因此猜测您在 32 位版本的 Windows 上,这就是它失败的原因。

C:\>set /a test=1+2
3

C:\>set /a test=1+217522712576
Invalid number.  Numbers are limited to 32-bits of precision.

Those numbers would overflow a 32 bit integer so guessing your on a 32 bit version of windows, that's why its failing.

C:\>set /a test=1+2
3

C:\>set /a test=1+217522712576
Invalid number.  Numbers are limited to 32-bits of precision.
微暖i 2024-12-17 09:17:56

正如其他人所说,数字太大了,但是如果你将它们保留为字符串并填充到相同的长度,它看起来会起作用

@echo off

rem cant do this get: Invalid number.  Numbers are limited to 32-bits of precision.
set Free=217522712576
set Need=2000000000

rem can do 
set Free=00000000000%Free%X
set free=%Free:~-13%

set Need=00000000000%Need%X
set Need=%Need:~-13%


echo Free=%Free%
echo Need=%Need%

echo on
IF %Free% GTR %Need% (GOTO Sufficient_Space) ELSE GOTO Insufficient_Space
@echo off

:Insufficient_Space
@ECHO INSUFFICIENT SPACE
GOTO DONE

:Sufficient_Space
@ECHO SUFFICIENT SPACE

:DONE

As others said the numbers are too big, however if you keep them as strings and pad out to be the same length, it looks to work

@echo off

rem cant do this get: Invalid number.  Numbers are limited to 32-bits of precision.
set Free=217522712576
set Need=2000000000

rem can do 
set Free=00000000000%Free%X
set free=%Free:~-13%

set Need=00000000000%Need%X
set Need=%Need:~-13%


echo Free=%Free%
echo Need=%Need%

echo on
IF %Free% GTR %Need% (GOTO Sufficient_Space) ELSE GOTO Insufficient_Space
@echo off

:Insufficient_Space
@ECHO INSUFFICIENT SPACE
GOTO DONE

:Sufficient_Space
@ECHO SUFFICIENT SPACE

:DONE
乖不如嘢 2024-12-17 09:17:56

请注意,CMD 的精度介于 -2^312^31-1 之间
等于 -2 147 483 6482 147 483 647
如果小于或大于限制
出现警告:号码无效。数字的精度限制为 32 位。

Note That CMD Had A precision betweeb -2^31 to 2^31-1
that equal to -2 147 483 648 to 2 147 483 647
If smaller of bigger than the limit
a warning came:Invalid number. Numbers are limited to 32-bits of precision.

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