很多时候我们需要清理文件时需要分析文件夹大小并排序,但默认文件浏览器没有这个功能,虽然有像foldersize,tc等软件有类似的功能,但每次安装这些软件有些不便,所以写了这个脚本用系统默认装好的bat和powershell分析文件夹大小并排序,放在某个目录运行会分析该文件夹宁且可以进步退步分析文件夹
@echo off&setLocal EnableDelayedExpansion
chcp 65001>nul
color 03
:main
echo 正在分析%cd%文件夹,请稍等...
set "of=%userprofile%\learn\sorttemp"
if exist %of% (rd /s /q %of%)
mkdir %of%
for /f "delims=" %%i in ('dir /ad /b ^| find /c /v ""') do set folder_count=%%i
for /f "delims=" %%i in ('dir /a-d /b ^| find /c /v ""') do set file_count=%%i
echo | set /p dummyName=大小: >>%of%\totalfoldersize.txt
powershell -command "$totalsize=[long]0;gci %cd% -File -r -fo -ea Silent|ForEach-Object{$totalsize+=$_.Length};$totalsize">>%of%\totalfoldersize.txt
powershell -Command "$content = Get-Content -Path '%of%\totalfoldersize.txt' -Encoding UTF8; Set-Content -Path '%of%\totalfoldersize.txt' -Value $content -Encoding Unicode"
powershell -ExecutionPolicy Bypass -Command "Get-Content '%of%\totalfoldersize.txt' | ForEach-Object { $line = $_; $split = $line.Split(':'); $value = [double]$split[1]; if ($value -lt 1024) { $size = '{0:N3}' -f $value + ' B' } elseif ($value -lt 1048576) { $size = '{0:N3}' -f ($value / 1024) + ' KB' } elseif ($value -lt 1073741824) { $size = '{0:N3}' -f ($value / 1048576) + ' MB' } else { $size = '{0:N3}' -f ($value / 1073741824) + ' GB' }; Write-Output ($split[0] + ':' + $size) }| Out-File '%of%\totalf.txt' -Encoding UTF8"
for %%i in (*) do (
if %%~zi NEQ 0 (
set a/ count+=1
echo %%~nxi:%%~zi>>%of%\file.txt))
set "inputFile=%of%\file.txt"
for /d %%i in (*) do (
echo | set /p dummyName=[dir]%%i:
powershell -command "$totalsize=[long]0;gci %%i -File -r -fo -ea Silent|ForEach-Object{$totalsize+=$_.Length};$totalsize")>>%of%\file.txt
powershell -Command "$content = Get-Content -Path '%inputFile%' -Encoding UTF8; Set-Content -Path '%inputFile%' -Value $content -Encoding Unicode"
set "outputFile=%of%\sorted_file.txt"
set "outputFile1=%of%\sorted_file1.txt"
powershell -Command "(Get-Content '%inputFile%') | ForEach-Object { [regex]::Match($_, ':(.*)$').Groups[1].Value + ':' + $_ } | Sort-Object -Property @{ Expression={[double]($_ -split ':')[0]} } | ForEach-Object { $_ -replace '^[^:]+:' } | Out-File '%outputFile%' -Encoding UTF8"
powershell -Command "$content = Get-Content -Path '%outputFile%' -Encoding UTF8; Set-Content -Path '%outputFile%' -Value $content -Encoding Unicode"
powershell -ExecutionPolicy Bypass -Command "Get-Content '%outputFile%' | ForEach-Object { $line = $_; $split = $line.Split(':'); $value = [double]$split[1]; if ($value -lt 1024) { $size = '{0:N3}' -f $value + ' B' } elseif ($value -lt 1048576) { $size = '{0:N3}' -f ($value / 1024) + ' KB' } elseif ($value -lt 1073741824) { $size = '{0:N3}' -f ($value / 1048576) + ' MB' } else { $size = '{0:N3}' -f ($value / 1073741824) + ' GB' }; Write-Output ($split[0] + ':' + $size) }| Out-File '%outputFile1%' -Encoding UTF8"
for /f "tokens=1,2 delims=:" %%a in (%outputFile1%) do (
set "string=%%a"
set "byte=%%b"
for /f %%c in ('cmd /u /c echo %%string%%^|find /v /c ""') do set /a "byte_count=%%c*2"
rem 格式化第一个字符串
if "!string:~30!"=="" (
set "formatted_string=!string!"
set /a "space_count=60-byte_count/2"
for /l %%d in (1,1,!space_count!) do set "formatted_string=!formatted_string! "
) else (
set "formatted_string=!string:~0,10!.....!string:~-15!%5"
set /a "space_count=61-byte_count/2"
for /l %%d in (1,1,!space_count!) do set "formatted_string=!formatted_string! "
)
echo !formatted_string!!byte!
)
echo | set /p dummyName=当前文件夹是%cd%,其中有%file_count%个文件,%folder_count%个文件夹,
type %of%\totalf.txt
if exist %of% (rd /s /q %of%)
set /p pathnew=请输入需要分析文件夹名称([dir]表示文件夹,按..可分析上一个文件夹):
REM 判断路径是否存在
if %pathnew% equ ".." (
cd ..
call :main
)
if exist %cd%\%pathnew% (
cd %cd%\%pathnew%
call :main
)
call :main
goto :eof