如何批量将专辑文件夹从艺术家目录移至我的音乐根目录?
批处理实现专辑目录批量迁移
需求场景
用Windows Media Player翻录CD后,音乐文件默认目录结构如下:
My Music --Artist 1 ----Album 1 --Artist 2 ----Album 2 ----Album 3
需要将所有Album目录(包含内部文件)迁移到My Music根目录,最终结构变为:
My Music --Album 1 --Album 2 --Album 3
批处理解决方案
以下是可直接使用的批处理脚本,已考虑同名目录冲突、空目录清理等问题:
@echo off setlocal :: 遍历所有Artist目录 for /d %%a in ("*") do ( :: 遍历当前Artist下的所有Album目录 for /d %%b in ("%%a\*") do ( :: 检查目标位置是否已有同名目录,避免覆盖 if exist "%%~nxb" ( echo 警告:目录 "%%~nxb" 已存在,跳过迁移 ) else ( move "%%b" ".\" echo 已迁移:"%%b" → "My Music" 根目录 ) ) :: 删除空的Artist目录(非空则跳过) rd "%%a" 2>nul if not errorlevel 1 ( echo 已删除空目录:"%%a" ) ) echo 操作完成 pause
使用步骤
- 打开记事本,复制上述代码
- 保存为
MoveAlbums.bat(保存类型选择「所有文件」) - 将该批处理文件放到
My Music目录下 - 双击运行即可
原PowerShell脚本问题分析
你之前的脚本报错是因为Get-ChildItem *\*\*\*匹配的是Artist\Album下的文件而非Album目录本身。处理文件时,若路径层级异常会导致($_.Parent).Parent为空,进而引发destination参数错误。如果后续想尝试PowerShell,可使用以下修正版本:
# 迁移所有Album目录到My Music根目录 Get-ChildItem -Directory -Path .\*\* | ForEach-Object { $destPath = Join-Path $_.Parent.Parent.FullName $_.Name if (-not (Test-Path $destPath)) { Move-Item $_.FullName $destPath Write-Host "已迁移:$($_.Name)" } else { Write-Warning "目录 $($_.Name) 已存在,跳过" } } # 删除空的Artist目录 Get-ChildItem -Directory | Where-Object { (Get-ChildItem $_.FullName).Count -eq 0 } | Remove-Item
内容的提问来源于stack exchange,提问作者TI89Titanium
相关产品推荐
相关产品推荐

