How can I convert this raw bytes output to GB?

前端 未结 5 1906
陌清茗
陌清茗 2020-12-21 07:08

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 \"use         


        
相关标签:
5条回答
  • 2020-12-21 07:46

    Could use the StrFormatByteSize64() API function to convert a long int to human readable size. This results in a more accurate value than truncating to meet the cmd environment's 32-bit limit. This API function supports values in the ranges 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()
    
    0 讨论(0)
  • 2020-12-21 07:48

    Here is a solution that gives GB in a whole number. May not be what you wanted, but it was easy to do, and may do the trick for what you need. I couldn't really 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, here is the same thing using 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%
    
    0 讨论(0)
  • 2020-12-21 07:50

    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.
        )
    )
    
    0 讨论(0)
  • 2020-12-21 08:06

    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

    0 讨论(0)
  • 2020-12-21 08:09

    Must you use batch commands? Can you not use PowerShell?

    [System.IO.DriveInfo]::GetDrives() | Where {$_.Name -eq 'C:\'} |
    Select {$_.AvailableFreeSpace/1GB}
    
    0 讨论(0)
提交回复
热议问题