dmz社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1262|回复: 1

PowerShell扫描文件夹大小

[复制链接]

6

主题

707

帖子

2934

积分

傲视群雄

Rank: 8Rank: 8

积分
2934

发表于 2024-8-8 12:06:15 | 显示全部楼层 |阅读模式

本站资源全部免费,回复即可查看下载地址!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 LiuJiaCheng11 于 2024-8-8 12:20 编辑

某天我发现我的E盘快满了,想清理一些大文件,但又不想去网上下载什么工具,就自己整个脚本去扫描了
效果图1
10
效果图2
有个无权限的文件夹,扫不了会报错(但似乎文件大小是正确的),报错信息会在进度条完成,输出结果后,再输出有错误的文件信息
10
代码
[PowerShell] 纯文本查看 复制代码
function Get-ResolvePath {
    param (
        [string]$Path
    )
    
    $ResolvePath = Resolve-Path -Path $Path
    return $ResolvePath
}

function Get-FolderSize {
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$FolderPath
    )

    $FolderPath = Get-ResolvePath $FolderPath
    if (Test-Path -Path $FolderPath -PathType Leaf) {
        # 如果路径是文件,则直接返回文件大小
        $file = Get-Item -Path $FolderPath -Force
        return $file.Length
    }

    $folderSize = 0
    try {
        Get-ChildItem -Recurse -File -Path $FolderPath -Force -ErrorAction Stop | ForEach-Object {
            try {
                $folderSize += $_.Length
            } catch {
                $global:errors += "处理文件时出错: $($_.FullName) - 错误信息: $($_.Exception.Message)"
            }
        }
    } catch {
        $global:errors += "解析目录时出错: $FolderPath - 错误信息: $($_.Exception.Message)"
    }
    
    return $folderSize
}

do {
    # 初始化错误数组
    $global:errors = @()

    # 读取用户输入的文件夹路径
    $folderPath = Read-Host "请输入文件夹路径(空表示当前路径)"
    if ($folderPath -eq ""){
        $folderPath = $PWD.Path
    }
    
    $subFolders = Get-ChildItem -Path $folderPath -Force
    $totalFolders = $subFolders.Count
    $currentFolder = 0

    $sortedFolders = $subFolders | ForEach-Object {
        $currentFolder++
        $progressPercent = [math]::Round(($currentFolder / $totalFolders) * 100, 2)

        # 更新进度条
        Write-Progress -Activity "正在处理目录" -Status "$($_.FullName)" -PercentComplete $progressPercent

        # 获取文件夹的大小
        $folderSize = Get-FolderSize -FolderPath $_.FullName
        [PSCustomObject]@{
            FolderName = $_.Name
            FolderSize = $folderSize
        }
    } | Sort-Object -Property FolderSize -Descending
    
    # 进度条完成
    Write-Progress -Activity "处理完成" -Status "所有目录已处理" -Completed

    # 显示文件夹名称和大小
    $sortedFolders | ForEach-Object {
        if (![string]::IsNullOrEmpty($_.FolderName)) {
            if ($_.FolderSize -ge 1GB) {
                $formattedSize = "{0:N2} GB" -f ($_.FolderSize / 1GB)
            } else {
                if ($_.FolderSize -ge 1MB) {
                    $formattedSize = "{0:N2} MB" -f ($_.FolderSize / 1MB)
                } else {
                    $formattedSize = "{0:N2} KB" -f ($_.FolderSize / 1KB)
                }
            }
            Write-Host "$($formattedSize) - $($_.FolderName)"
        }
    }

    # 输出错误信息
    if ($global:errors.Count -gt 0) {
        Write-Host "处理过程中出现以下错误:" -ForegroundColor Red
        $global:errors | ForEach-Object { Write-Host $_ -ForegroundColor Red }
    }
    
    $continue = Read-Host "是否继续循环?(Y/N)"
} while ($continue -eq "Y" -or $continue -eq "y")






回复

使用道具 举报

6

主题

707

帖子

2934

积分

傲视群雄

Rank: 8Rank: 8

积分
2934

 楼主| 发表于 2024-8-8 12:11:54 | 显示全部楼层
本帖最后由 LiuJiaCheng11 于 2024-8-8 12:18 编辑

最大的居然是微信文件夹 image.png

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|小黑屋|dmz社区

GMT+8, 2026-7-29 06:17 , Processed in 0.041446 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表