【发布时间】:2015-03-26 00:51:29
【问题描述】:
我的 Windows 机器上有一堆音乐,它们按艺术家和专辑组织在文件夹中。我想将主目录下所有文件夹和子文件夹中的所有 mp3 递归复制到一个位置。
这可以用windows命令行完成吗?
【问题讨论】:
-
我推荐使用powershell来做这个,它预装了windows。
-
谢谢。你能告诉我语法是什么吗?
标签: windows-console
我的 Windows 机器上有一堆音乐,它们按艺术家和专辑组织在文件夹中。我想将主目录下所有文件夹和子文件夹中的所有 mp3 递归复制到一个位置。
这可以用windows命令行完成吗?
【问题讨论】:
标签: windows-console
假设我们有以下结构
我们希望将所有这些文件复制到
中的单个目录在 c:\temp\ 中创建一个 bat 文件,例如 copyallmp3.bat 并编写以下代码
for /R "C:\temp" %%i in (*.mp3) do xcopy "%%i" "C:\temp\all" /y
运行 copyallmp3.bat。 如果您导航到 c:\temp\all,您将看到所有 4 个 mp3 文件。
如果您希望最终结果类似于(递归)
在powershell中使用以下代码
$Source = 'C:\temp'
$Files = '*.mp3'
$Dest = 'C:\temp\all'
Get-ChildItem $Source -Filter $Files -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Copy-Item $_.FullName -Destination $Path -Force
}}
【讨论】:
您的代码启发了我做类似的任务。我将目录和文件包含到我的新目的地的文件格式列表。然后我之后删除空文件夹。
$Source = 'D:\Téléchargements'
$Dest = 'D:\Musique'
# List of file format
$File = '*.mp3', '*.flac'
Get-ChildItem "$Source\*" -Include $File -Recurse –force -ErrorAction SilentlyContinue | ForEach{
$FilePath = $_.DirectoryName
# File directly in source dir
If ($FilePath -eq $Source) { Move-Item $_.FullName -Destination $Dest -Force }
Else {
$DestPath = ("$FilePath\") -Replace [Regex]::Escape($Source), $Dest
If ( !(Test-Path $DestPath) ) { New-Item -ItemType Directory -Path $DestPath -Force | Out-Null }
Move-Item $_.FullName -Destination $DestPath -Force
# Delete empty dir
If ((Get-ChildItem $FilePath).count -eq 0) { Remove-Item $FilePath -Force}
}
}
【讨论】:
在 windows 控制台中的命令是
xcopy "*".mp3 $from $to
$from它应该是你的目标源的路径
$to它应该是你的来源的新路径
【讨论】:
假设您的目录是结构化的artist/album/*.mp3:
为复制的文件创建一个目录
mkdir new
转到您的音乐目录
cd g:/music
在您的.mp3 后面加上三个通配符*,以\ 分隔:
cp "*/*/*.mp3" "g:/new"
【讨论】: