前言

代码占用的空间越来越大了,写了个脚本清理一下 dist 和 .git

脚本

清理脚本

Get-ChildItem -Directory -Force | ForEach-Object {
    $folder = $_.FullName

    # 检查 dist 文件夹
    $distPath = Join-Path $folder 'dist'
    if (Test-Path $distPath ) {
        if ($folder -notlike "*node_modules*") {
            Write-Host "检测到 dist 文件夹: $distPath"

            # 删除 dist 文件夹下的所有内容,但保留 dist 文件夹本身
            Get-ChildItem -Path $distPath -Force | Remove-Item -Recurse -Force
            Write-Host "dist清理完成"
        }
    }

    # 检查 .git 文件夹
    $gitPath = Join-Path $folder '.git'
    if (Test-Path $gitPath ) {
        Write-Host "检测到 .git 文件夹: $gitPath"
        Push-Location $folder
        git gc
        git remote prune origin
        $branches = git branch -vv | Select-String 'origin/.*: gone]' | ForEach-Object {
            ($_ -split '\s+')[1]
        }
        foreach ($branch in $branches) {
            if ($branch -ne 'main' -and $branch -ne 'master') {
                git branch -D $branch
                Write-Host "已删除本地分支: $branch"
            }
        }
        Write-Host "git 清理完成。"
        Pop-Location
    }
    else {
        Write-Host "未检测到 .git 文件夹,跳过 git 清理。"
    }
}

查看文件夹大小脚本

$folderSizes = @{}

Get-ChildItem -Directory -Force | ForEach-Object {
    $folder = $_.FullName
    $size = (Get-ChildItem -Path $folder -Recurse -Force | Measure-Object -Property Length -Sum).Sum
    $sizeMB = [math]::Round($size / 1MB, 2)
    $folderSizes[$folder] = $sizeMB
    Write-Host "文件夹: $folder, 大小: $sizeMB MB"
}

Write-Host "==========================="
Write-Host "==========================="
Write-Host "==========================="


# 按大小从大到小排序并输出
$folderSizes.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object {
    Write-Host "文件夹: $($_.Key), 大小: $($_.Value) MB"
}

执行效果

执行前 | 执行后
2025-07-03T02:06:36.png

最后修改:2025 年 07 月 03 日
如果觉得我的文章对你有用,请随意赞赏