You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改BAT代码同时输出文件文件夹大小并排序 或提供PowerShell方案

实现方案

推荐:PowerShell版本(更简洁稳定)

完全满足所有需求:

  • 同时获取指定路径下所有一级文件、文件夹的大小
  • 大小<1GB时以MB为单位显示,≥1GB时以GB为单位显示,保留1位小数
  • 输出结果自动按字母顺序排序
  • 无需临时VBS文件、运行效率更高
# 自定义配置路径
$InputFolder = "F:\Input_Folder"
$OutputFile = "F:\Output_Folder\WS_list.txt"

# 读取路径下所有一级文件和文件夹
$allItems = Get-ChildItem -Path $InputFolder -Force

$result = foreach ($item in $allItems) {
    # 计算大小:文件夹递归统计总大小,文件直接取自身大小
    if ($item.PSIsContainer) {
        $totalSize = (Get-ChildItem -Path $item.FullName -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
    }
    else {
        $totalSize = $item.Length
    }

    # 单位转换
    if ($totalSize -ge 1GB) {
        $formattedSize = [math]::Round($totalSize / 1GB, 1)
        $unit = "GB"
    }
    else {
        $formattedSize = [math]::Round($totalSize / 1MB, 1)
        $unit = "MB"
    }

    # 拼接输出内容
    "$($item.Name) - $formattedSize $unit"
}

# 按字母排序后写入输出文件
$result | Sort-Object | Out-File -FilePath $OutputFile -Encoding UTF8

修改后的BAT批处理版本

针对原有代码做了以下调整:

  • 新增一级文件的大小统计逻辑
  • 所有条目生成后自动按字母排序输出
  • 保留原有单位转换逻辑不变
@echo off
setlocal disabledelayedexpansion

set "InputFolder=F:\Input_Folder"
set "OutputFolder=F:\Output_Folder"
set "TempFolder=F:\Temp"
if not defined InputFolder set "InputFolder=%cd%"
del /q "%TempFolder%\temp_list.txt" 2>nul

:: 遍历一级文件夹统计大小
for /d %%a in ("%InputFolder%\*") do (
    set "size=0"
    for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"
    setlocal enabledelayedexpansion
    call :GetUnit !size! unit
    call :ConvertBytes !size! !unit! newsize
    echo(%%~nxa - !newsize! !unit!
    endlocal
) >> "%TempFolder%\temp_list.txt"

:: 新增:遍历一级文件统计大小
for %%a in ("%InputFolder%\*") do (
    if not exist "%%~fa\" (
        set "size=%%~za"
        setlocal enabledelayedexpansion
        call :GetUnit !size! unit
        call :ConvertBytes !size! !unit! newsize
        echo(%%~nxa - !newsize! !unit!
        endlocal
    )
) >> "%TempFolder%\temp_list.txt"

:: 按字母排序后输出到最终文件
sort "%TempFolder%\temp_list.txt" /o "%OutputFolder%\WS_list.txt"
del /q "%TempFolder%\temp_list.txt" 2>nul

endlocal
pause
exit /b

:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~2" EQU "TB" set val=/1024/1024/1024/1024
> "%TempFolder%"\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)
for /f "delims=" %%a in ( 
  'cscript //nologo "%TempFolder%"\tmp.vbs' 
) do endlocal & set %~3=%%a
del "%TempFolder%"\tmp.vbs
exit /b


:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %1 LEQ 1024 set "unit=Bytes"
if %1 GTR 1024   set "unit=KB"
if %1 GTR 1048576  set "unit=MB"
if %1 GTR 1073741824  set "unit=GB"
if %byt:~-14% GTR %TB:~-14% set "unit=TB"
endlocal & set %~2=%unit%
exit /b

内容的提问来源于stack exchange,提问作者goldenbutter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 13:54:06