我怎么能这样原始字节输出转换为国标? [英] How can I convert this raw bytes output to GB?

查看:149
本文介绍了我怎么能这样原始字节输出转换为国标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code输出在C当前可用空间:驱动器。我怎么能转换输出字节GB使用批处理?

 关闭@echo
FOR / F有usebackq delims ==令牌= 2%%倍In(`WMIC逻辑磁盘其中的DeviceID =C:获得可用空间/格式:value`)做设置自由空间= %%点¯x
回声%可用空间%


解决方案

批不支持浮点运算。这将是一个不错的解决方法:

  @setlocal ENABLEEXTENSIONS enabledelayedexpansion
关闭@echoFOR / F有usebackq delims ==令牌= 2%%倍In(`WMIC逻辑磁盘其中的DeviceID =C:获得可用空间/格式:value`)做设置自由空间= %%点¯x呼应自由空间:〜0,-10,自由空间:!!!〜2日 - 8 GB

这仅当您运行的.bat以管理员身份工作。它只是从插入右侧的9个数字后一个点,修剪最后7.这是不完全从Windows的价值相匹配,因为1K这里是1000而不是1024

有一个更好,但更复杂的解决办法是使用VBScript,下面的文章中描述的:<一href=\"http://www.celticproductions.net/articles/11/batch+files/add+floating+point+numbers+in+batch+file.html\"相对=nofollow>文章

I'm using the below code to output the current free space on the C: Drive. How can I convert the output from bytes to GB using batch?

@echo off
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get       FreeSpace /format:value`) do set FreeSpace=%%x
echo %FreeSpace%

解决方案

Batch does not support float point arithmetic. This would be a nice workaround:

@setlocal enableextensions enabledelayedexpansion
@echo off

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get       FreeSpace /format:value`) do set FreeSpace=%%x

echo !FreeSpace:~0,-10!,!FreeSpace:~2,-8!GB

It only works if you run the .bat as administrator. It just inserts a dot after the 9. digits from the right, and trims the last 7. This is not exactly matching the value from windows, because 1k is here 1000 and not 1024

A better but more complex solution would be to use VBScript, described in the following article: Article

这篇关于我怎么能这样原始字节输出转换为国标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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