登录|使用QQ帐号登录
论坛 > 各类教程
发帖|
看320|回0|收藏
小樱 看全部
2025/12/5 01:59
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,比如发生跳过后文件被删了,以下是修复后可用的最终版本

代码:

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

  6. # 遍历根目录下的所有一级子文件夹 (A)
  7. Get-ChildItem -LiteralPath $rootDir -Directory | ForEach-Object {
  8.     $itemA = $_ # 当前处理的一级文件夹 A
  9.     $pathA = $itemA.FullName
  10.     $nameA = $itemA.Name
  11.    
  12.     Write-Host "----------------------------------------------------"
  13.     Write-Host "正在检查父文件夹: '$nameA'"
  14.    
  15.     # 查找 A 内部是否存在一个与自身同名的子文件夹 (A/A)
  16.     $duplicateFolderB = Get-ChildItem -LiteralPath $pathA -Directory | Where-Object { $_.Name -eq $nameA }
  17.    
  18.     if ($duplicateFolderB) {
  19.         $pathB = $duplicateFolderB.FullName
  20.         Write-Host "发现重复子文件夹 B: '$pathB'" -ForegroundColor Cyan
  21.         
  22.         # 目标:将子文件夹 B 内部的所有内容向上移动到 父文件夹 A
  23.         $targetContents = Get-ChildItem -LiteralPath $pathB -Force
  24.         $moveSuccessful = $true # 标记是否所有内容都已处理
  25.         
  26.         if ($targetContents.Count -gt 0) {
  27.             Write-Host "发现 B 内部有 $($targetContents.Count) 项内容,准备向上移动..."
  28.             
  29.             try {
  30.                 # 1. 移动子文件夹 B 内部的所有内容到父文件夹 A
  31.                 foreach ($content in $targetContents) {
  32.                     $destinationPath = Join-Path -Path $pathA -ChildPath $content.Name
  33.                     
  34.                     # 检查目标文件夹 A 中是否已存在相同名称的文件/文件夹,防止冲突
  35.                     if (Test-Path -LiteralPath $destinationPath) {
  36.                         Write-Host "警告: 父文件夹 A 中已存在 '$($content.Name)',跳过移动此项。" -ForegroundColor Yellow
  37.                         $moveSuccessful = $false
  38.                         continue
  39.                     }
  40.                     
  41.                     # 移动操作
  42.                     Write-Host "    - 移动: '$($content.Name)'"
  43.                     Move-Item -LiteralPath $content.FullName -Destination $pathA -Force -ErrorAction Stop
  44.                 }
  45.                
  46.                 # 2. **修正后的安全删除:检查 B 是否为空并删除它**
  47.                 # 只有当 B 文件夹内部没有剩余内容时,才删除它
  48.                 $remainingContents = Get-ChildItem -LiteralPath $pathB -Force
  49.                 if ($remainingContents.Count -eq 0) {
  50.                     Remove-Item -LiteralPath $pathB -Recurse -Force -ErrorAction Stop
  51.                     Write-Host "成功消除重复层级 A\A,并删除空文件夹 B。" -ForegroundColor Green
  52.                 } else {
  53.                     Write-Host "错误: 重复子文件夹 B 中仍有 $($remainingContents.Count) 项内容,保留 B。" -ForegroundColor Red
  54.                     Write-Host "请手动检查 B 文件夹: '$pathB'" -ForegroundColor Red
  55.                     $remainingContents | ForEach-Object { Write-Host "   - 保留项: $($_.Name)" -ForegroundColor Red }
  56.                 }

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

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


IT技术交流论坛

Powered by itzmx!

首页|标准版|触屏版|电脑版