How do I convert this raw byte output to GB?
I am using the below code to output the current free space in C: Drive. How can I convert the output from bytes to GB using a 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%
source to share
The package does not support floating point arithmetic. This would be a good solution:
@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 when running .bat as administrator. It just inserts a dot after the 9. digits to the right and truncates the last 7. This doesn't quite match the value from the windows because 1k is 1000 here, not 1024
A better but more complex solution would be to use VBScript described in the following article: Article
source to share
Here is a solution that gives GB as a whole. It might not be what you wanted, but it was easy to do and might do the trick for what you need. I couldn't get it to work for me using wmic, but wmic is probably better than 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%
Eh, well, that's the same as 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%
source to share
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.
)
)
source to share
You can use the StrFormatByteSize64()
API function to convert a long int to a human readable size. This results in a more accurate value than truncation to fit the 32-bit environment limit cmd
. This API function supports values that range from bytes and kilobytes to petabytes and exabytes.
(This script is a hybrid Batch + PowerShell script. Save it with a .bat extension .)
<# : 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()
source to share