如何将FORFILES @fsize输出转换为GB?

时间:2014-06-06 12:40:39

标签: file batch-file size

我需要远程了解特定文件的大小,

这个想法是输入类似的东西:

"ip address" batchfile.cmd

期望的输出:

fileName1.ost size 10GB
fileName2.ost size 20GB
fileName3.ost size 20GB
fileName4.ost size 20GB

这些代码有效,但大小只以字节显示

C:\>forfiles /P %userprofile% /M *.ost /S /C "cmd /c echo @file @fsize"
非常感谢

1 个答案:

答案 0 :(得分:2)

只是一个粗略的解决方案。不是确切的计算,但对于你的问题,它可能就足够了

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /r "%userprofile%" %%a in ("*.ost") do (
        set "size=%%~za" & set "unit=B"
        for %%b in (KB MB GB TB PB EB ZB YB) do if 1024 lss !size! (
            set "size=!size:~0,-3!" & set "unit=%%b"
        )
        echo %%~fa %%~za !size!!unit! 
    )

    endlocal

已编辑有关更精确的版本

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /r "%userprofile%" %%a in ("*.ost") do (
        set "size=%%~za" & set "unit=B"
        for %%b in (KB MB GB TB PB EB ZB YB) do if 1024 lss !size! (
            set "unit=%%b" 
            if  !size! lss 2147483647 (set /a "size=size/1024") else (set "size=!size:~0,-3!")
        )
        echo %%~fa %%~za !size!!unit! 
    )

    endlocal