
标题: [数值计算] [已解决]批处理用powershell换算硬盘容量的函数,怎么再优化一下 [打印本页]
作者: hnfeng 时间: 2025-2-11 14:04 标题: [已解决]批处理用powershell换算硬盘容量的函数,怎么再优化一下
本帖最后由 hnfeng 于 2025-2-12 08:10 编辑
写了个根据数值(硬盘容量),换算为适当单位(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 :EOF
复制代码
[img]
[/img]
作者: Five66 时间: 2025-2-11 15:30
- :getTGMK
- setlocal
- for /f "usebackq" %%c in (`powershell -c "$i=%~1;('TB',1tb),('GB',1gb),('MB',1mb),('KB',1kb),('B',1)|foreach{if($i -ge $_[1]){''+[math]::Truncate($i/$_[1])+$_[0];break}}"`) do (
- endlocal & set %~2=%%c&goto :EOF
- )
复制代码
作者: hnfeng 时间: 2025-2-11 15:46
Five66 发表于 2025-2-11 15:30 
多谢,就是这个意思。
不过想着可能速度会快点,比较后感觉速度差不多
作者: aloha20200628 时间: 2025-2-11 18:15
本帖最后由 aloha20200628 于 2025-2-11 20:16 编辑
回复 1# hnfeng
先用 wmic 获取全部硬盘数据再一次性丢给 powershell 完成大数计算整理,效率会好一些...- @echo off &echo,磁盘 剩余 总大小
- wmic logicaldisk get Caption^,Size^,FreeSpace|findstr "[0-9][0-9]*"|powershell "$input|%%{$a=$_ -split '[ ]+';if($a.count -gt 1){$x,$y,$ux,$uy=[int64]$a[1],[int64]$a[2],'GB','GB';if($x -ge 1tb){$x/=1tb;$ux='TB'}else{$x/=1gb};if($y -ge 1tb){$y/=1tb;$uy='TB'}else{$y/=1gb};$a[0]+"""`t"""+$x.tostring(0)+$ux+"""`t"""+$y.tostring(0)+$uy}}"
- pause&exit/b
复制代码
作者: Five66 时间: 2025-2-11 19:39
回复 3# hnfeng
要速度就不要分开来 比如一次性处理所有数据
或者
不要用powershell ,按启动1秒来算 ,换成其他的话 ,每次调用应该能快半秒
或者
不要用call ,能够快一点点的时间- @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 (
- set "str=!str! %%a %%b %%c?"
- )
-
- echo,!str!|powershell -c "$j=$input -split '\?';$j|foreach{[regex]::replace($_,'\d+',{$i=[decimal]$args[0].value;('TB',1tb),('GB',1gb),('MB',1mb),('KB',1kb),('B',1)|foreach{if($i -ge $_[1]){'{0,7}' -f (''+[math]::Truncate($i/$_[1])+$_[0]);$i=0;}}})}"
-
- pause
- endlocal&exit/b
复制代码
作者: hnfeng 时间: 2025-2-12 08:09
回复 hnfeng
先用 wmic 获取全部硬盘数据再一次性丢给 powershell 完成大数计算整理,效率会好一些...
aloha20200628 发表于 2025-2-11 18:15 
非常感谢,厉害
作者: hnfeng 时间: 2025-2-12 08:09
回复 hnfeng
要速度就不要分开来 比如一次性处理所有数据
或者
不要用powershell ,按启动1秒 ...
Five66 发表于 2025-2-11 19:39 
非常感谢,学习了
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |