如何将此原始字节输出转换为GB?

时间:2013-02-04 15:46:14

标签: batch-file

我正在使用以下代码输出C:Drive上的当前可用空间。如何使用批处理将输出从字节转换为GB?

@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%

5 个答案:

答案 0 :(得分:2)

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

@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

仅当您以管理员身份运行.bat时才有效。它只是在右边的9.数字后插入一个点,并修剪最后的7.这与windows的值不完全匹配,因为1k在这里是1000而不是1024

更好但更复杂的解决方案是使用VBScript,如下文所述:Article

答案 1 :(得分:1)

这是一个给出整数GB的解决方案。可能不是你想要的,但它很容易做到,并且可以根据你的需要来做。我无法让它为我使用wmic工作,但是wmic可能比dir更好。

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=3" %%a in ('dir c:\') do (
    set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
endlocal && set bytesfree=%bytesfree%

rem truncating end. loses precision
set /a kb=%bytesfree:~0,-3%
set /a mb = kb/1024
set /a gb = mb/1024
echo %gb%

嗯,这是使用wmic的同样的事情。

@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

rem truncating end. losing precision
set /a kb=%FreeSpace:~0,-4%
set /a mb = kb/1024
set /a gb = mb/1024
echo %gb%

答案 2 :(得分:0)

您必须使用批处理命令吗?你能不能使用PowerShell?

[System.IO.DriveInfo]::GetDrives() | Where {$_.Name -eq 'C:\'} |
Select {$_.AvailableFreeSpace/1GB}

答案 3 :(得分:0)

REM ECHO Disk Storage

for /f "tokens=1" %%d in (
 'wmic logicaldisk where drivetype^=3 get deviceid ^| find ":"') do ( 
    for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%%d'" get Size /value`) do set Size=%%x
    echo VolumeSize on %%d Partition = !Size:~0,-10!,!Size:~2,-8! GB >output.txt
    for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%%d'" get FreeSpace /value`) do set FreeSpace=%%x
    echo Freespace on %%d Partition = !FreeSpace:~0,-10!,!FreeSpace:~2,-8! GB >> output.txt
    echo.
    )
)

答案 4 :(得分:0)

可以使用StrFormatByteSize64() API function将long int转换为人类可读的大小。这会产生比截断更准确的值,以满足cmd环境的32位限制。此API函数支持从字节和千字节到千兆字节和艾字节的范围内的值。

(此脚本是混合Batch + PowerShell脚本。使用 .bat 扩展名保存。)

<# : batch portion
@echo off & setlocal

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

echo %FreeSpace% free on C:

rem // end main runtime
exit /b

rem // batch int2size function
:int2size <return_varname> <int>
setlocal
set "num=%~2"
for /f "delims=" %%I in (
    'powershell -noprofile "iex (${%~f0} | out-string)"'
) do endlocal & set "%~1=%%I" & goto :EOF

: end batch / begin PowerShell #>
Add-Type @'
using System.Runtime.InteropServices;
namespace shlwapi {
    public static class dll {
        [DllImport("shlwapi.dll")]
        public static extern long StrFormatByteSize64(ulong fileSize,
            System.Text.StringBuilder buffer, int bufferSize);
    }
}
'@

$sb = new-object Text.StringBuilder 16
[void][shlwapi.dll]::StrFormatByteSize64($env:num, $sb, 16)
$sb.ToString()