批处理文件比较数字 [英] Batch files compare numbers

查看:315
本文介绍了批处理文件比较数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个脚本,检查驱动器中是否有足够的空间。
我写了以下内容:

  @echo off 
set gbsize = 1,073,741,824
Set gbsize =%gbsize:,=%
for / ftokens = 3%% a in('dir c:\')do(
set / a bytesfree = %% a

set bytesfree =%bytesfree:,=%
endlocal&& set bytesfree =%bytesfree%

如果%gbsize%gtr%bytesfree%echo hi

但是当脚本抛出错误:
无效的数字。数字常数是十进制(17),
十六进制(0x11)或八进制(021)。



可以任何人帮助?
谢谢!

解决方案

您的直接问题是在您的值中有逗号时尝试使用SET / / p>

您还需要在定义gbsize时删除before和after =之后的空格。



为什么你有endlocal没有setlocal



我相信这是你正在寻找。如果有少于1GB的可用空间,它将回显hi。

  @echo off 
setlocal
set gbsize = 1,073,741,824
设置gbsize =%gbsize:,=%
为/ ftokens = 3%% a in('dir c:\')do set bytesfree = %% a
set bytesfree =%bytesfree:,=%
如果%gbsize%gtr%bytesfree%echo hi


b $ b

另一种更直接(可能更准确的)获取自由空间的方式

  ftokens = 2%% S in('wmic volume get DriveLetter ^,FreeSpace ^ | findC:')do set bytesfree = %% S 


I have to write a script that check if there is enough space in the drive. I wrote the following:

@echo off
set gbsize = 1,073,741,824
Set gbsize=%gbsize:,=%
for /f "tokens=3" %%a in ('dir c:\') do (
    set /a bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
endlocal && set bytesfree=%bytesfree%

If %gbsize% gtr %bytesfree% echo hi

but when the script thrown error: Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).

what did I miss? can anyone help? thanks!

解决方案

Your immediate problem is attempting to use SET /A when there are commas in your value.

You also need to remove the spaces before and after = when you define gbsize.

I don't understand why you have endlocal without a setlocal

I believe this is what you were looking for. It will echo "hi" if there is less then 1GB free space.

@echo off
setlocal
set gbsize=1,073,741,824
Set gbsize=%gbsize:,=%
for /f "tokens=3" %%a in ('dir c:\') do set bytesfree=%%a
set bytesfree=%bytesfree:,=%
If %gbsize% gtr %bytesfree% echo hi

Another, more direct, (and possibly more accurate?) way to get the free space

for /f "tokens=2" %%S in ('wmic volume get DriveLetter^, FreeSpace^|find "C:"') do set bytesfree=%%S

这篇关于批处理文件比较数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆