在 DOS 批处理中比较 2 个数字不起作用
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些数字会溢出 32 位整数,因此猜测您在 32 位版本的 Windows 上,这就是它失败的原因。
Those numbers would overflow a 32 bit integer so guessing your on a 32 bit version of windows, that's why its failing.
正如其他人所说,数字太大了,但是如果你将它们保留为字符串并填充到相同的长度,它看起来会起作用
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
请注意,CMD 的精度介于
-2^31
到2^31-1
之间等于
-2 147 483 648
到2 147 483 647
如果小于或大于限制
出现警告:
号码无效。数字的精度限制为 32 位。
Note That CMD Had A precision betweeb
-2^31
to2^31-1
that equal to
-2 147 483 648
to2 147 483 647
If smaller of bigger than the limit
a warning came:
Invalid number. Numbers are limited to 32-bits of precision.