
[已解決]批處理用powershell換算硬盤(pán)容量的函數(shù)怎么再優(yōu)化一下最后由 hnfeng 于 -2-12 08:10寫(xiě)了個(gè)根據(jù)數(shù)值(硬盤(pán)容量),換算為適當(dāng)單位(TB,GB,MG,KB,B),顯示更直觀(guān)。但是不熟悉powershell,感覺(jué)函數(shù)部分應(yīng)該能再優(yōu)化一個(gè)請(qǐng)高手幫忙
[img]nload="thumbImg(this)" alt="" />[/img]nclick="copycode($('code0'));">復(fù)制代碼
- @echo off&setlocal EnableDelayedExpansion&cd /d %~dp0&title %~nx0
- echo磁盤(pán)剩余 總大小
- 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 :EOF

