小樱 发表于 2025/12/5 01:59

winrar右键批量解压后套了一层重复名称的文件夹,ps1批处理向上移动解决A\A

winrar右键批量解压后套了一层重复名称的文件夹,ps1批处理向上移动解决A\A

用7z右键菜单批量解压就没这种现象,但是winrar不知道为什么会这样,看起来是把rar压缩包的文件名用来创建一个文件夹,然后就导致重复了

例如解压后变成了
D:\test\A\A\B\C

莫名其妙的多了一层A,需要移动改回去,消除重复的A
D:\test\A\B\C

创建一个ps1文件,放在D:\test目录,然后运行
自己先复制几个文件夹来测试,这一份批处理代码是ai写的,我自己试了下确实好使,也手动给ai代码修了几个bug,比如发生跳过后文件被删了,以下是修复后可用的最终版本

# 设置要操作的根目录
$rootDir = "D:\test"
Write-Host "--- 目标根目录: $rootDir ---"
Write-Host "开始处理重复层级 (结构: A\A...) - **已启用安全删除检查**"
Write-Host ""

# 遍历根目录下的所有一级子文件夹 (A)
Get-ChildItem -LiteralPath $rootDir -Directory | ForEach-Object {
    $itemA = $_ # 当前处理的一级文件夹 A
    $pathA = $itemA.FullName
    $nameA = $itemA.Name
   
    Write-Host "----------------------------------------------------"
    Write-Host "正在检查父文件夹: '$nameA'"
   
    # 查找 A 内部是否存在一个与自身同名的子文件夹 (A/A)
    $duplicateFolderB = Get-ChildItem -LiteralPath $pathA -Directory | Where-Object { $_.Name -eq $nameA }
   
    if ($duplicateFolderB) {
      $pathB = $duplicateFolderB.FullName
      Write-Host "发现重复子文件夹 B: '$pathB'" -ForegroundColor Cyan
      
      # 目标:将子文件夹 B 内部的所有内容向上移动到 父文件夹 A
      $targetContents = Get-ChildItem -LiteralPath $pathB -Force
      $moveSuccessful = $true # 标记是否所有内容都已处理
      
      if ($targetContents.Count -gt 0) {
            Write-Host "发现 B 内部有 $($targetContents.Count) 项内容,准备向上移动..."
            
            try {
                # 1. 移动子文件夹 B 内部的所有内容到父文件夹 A
                foreach ($content in $targetContents) {
                  $destinationPath = Join-Path -Path $pathA -ChildPath $content.Name
                  
                  # 检查目标文件夹 A 中是否已存在相同名称的文件/文件夹,防止冲突
                  if (Test-Path -LiteralPath $destinationPath) {
                        Write-Host "警告: 父文件夹 A 中已存在 '$($content.Name)',跳过移动此项。" -ForegroundColor Yellow
                        $moveSuccessful = $false
                        continue
                  }
                  
                  # 移动操作
                  Write-Host "    - 移动: '$($content.Name)'"
                  Move-Item -LiteralPath $content.FullName -Destination $pathA -Force -ErrorAction Stop
                }
               
                # 2. **修正后的安全删除:检查 B 是否为空并删除它**
                # 只有当 B 文件夹内部没有剩余内容时,才删除它
                $remainingContents = Get-ChildItem -LiteralPath $pathB -Force
                if ($remainingContents.Count -eq 0) {
                  Remove-Item -LiteralPath $pathB -Recurse -Force -ErrorAction Stop
                  Write-Host "成功消除重复层级 A\A,并删除空文件夹 B。" -ForegroundColor Green
                } else {
                  Write-Host "错误: 重复子文件夹 B 中仍有 $($remainingContents.Count) 项内容,保留 B。" -ForegroundColor Red
                  Write-Host "请手动检查 B 文件夹: '$pathB'" -ForegroundColor Red
                  $remainingContents | ForEach-Object { Write-Host "   - 保留项: $($_.Name)" -ForegroundColor Red }
                }

            }
            catch {
                Write-Host "严重错误: 移动或删除操作失败。" -ForegroundColor Red
                Write-Host "路径: '$pathA'" -ForegroundColor Red
                Write-Host "错误信息: $($_.Exception.Message)" -ForegroundColor Red
            }
      }
      else {
            # 文件夹 B 本身是空的,可以直接删除
            Remove-Item -LiteralPath $pathB -Recurse -Force -ErrorAction Stop
            Write-Host "发现重复子文件夹 B 为空,已直接删除。" -ForegroundColor Green
      }
    }
    else {
      Write-Host "未发现重复结构 A\A,跳过。"
    }
}

Write-Host ""
Write-Host "所有文件夹处理完成。"
Read-Host "按任意键退出..." # 确保窗口保持打开

页: [1]
查看完整版本: winrar右键批量解压后套了一层重复名称的文件夹,ps1批处理向上移动解决A\A