返回列表 发帖

[数值计算] 批处理用powershell换算硬盘容量的函数,怎么再优化一下

写了个根据数值(硬盘容量),换算为适当单位(TB,GB,MG,KB,B),显示更直观。
但是不熟悉powershell,感觉函数部分应该能再优化一个
请高手帮忙
@echo off&setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0
echo  磁盘    剩余 总大小
for /f "tokens=1,2,3 delims= " %%a in ('wmic logicaldisk get Caption^,Size^,FreeSpace ^|findstr [0-9] ') do (
  rem echo %%a %%b %%c
  call :getTGMK %%b Size
  set "Size=       !Size!"
  set Size=!Size:~-7!
  call :getTGMK %%c FreeSpace
  set "FreeSpace=       !FreeSpace!"
  set FreeSpace=!FreeSpace:~-7!
  echo   %%a !Size! !FreeSpace!
)
echo;&pause
exit
:getTGMK
setlocal
set S=000000000000000000%1
set S=%S:~-18%
if "%S%" GEQ "000001099511627776" (
  for /f %%c in ('powershell -c "[Math]::Truncate(%1/1099511627776)"') do (endlocal & set %2=%%cTB&goto :EOF)
  ) else (
  if "%S%" GEQ "000000001073741824" (
    for /f %%c in ('powershell -c "[Math]::Truncate(%1/1073741824)"') do (endlocal & set %2=%%cGB&goto :EOF)
    ) else (
    if "%S%" GEQ "000000000001048576" (
      for /f %%c in ('powershell -c "[Math]::Truncate(%1/1048576)"') do (endlocal & set %2=%%cMB&goto :EOF)
      ) else (
      if "%S%" GEQ "000000000000001024" (
        for /f %%c in ('powershell -c "[Math]::Truncate(%1/1024)"') do (endlocal & set %2=%%cKB&goto :EOF)
        ) else (
        endlocal & set %2=%1B&goto :EOF
))))
goto :EOFCOPY
[img][/img]

返回列表