仅包含一个 MP3 的文件夹

仅包含一个 MP3 的文件夹

我对编码一无所知,我希望有办法满足我的需求,任何帮助都非常感谢。我有数千个包含 MP3 的文件夹。我想要做的是找到只有 1 个 MP3 的文件夹。然后我想将这些文件移动到一个特定的文件夹中。这有意义吗?有没有简单的方法可以做到这一点?我一直在手动操作,但这非常繁琐……

答案1

这应该可以做到,您可以在 PowerShell 中用一行完成此操作。

要打开 PowerShell,请执行以下操作:

Win+ →R类型powershellCtrl++ShiftEnter

然后粘贴以下代码(点击之前替换路径Enter):

(get-childitem -path "C:\path\to\folder" -directory -force -recurse).fullname | where {(get-childitem -path $_ -file -filter "*.mp3").count -eq 1}

然后您将能够找到仅包含一个 .mp3 文件的文件夹(但它们可能包含非 .mp3 文件的其他文件)。

请注意,此答案仅在您使用 Windows 时才有效,大概您使用 Windows 是基于它的受欢迎程度和您的非极客身份,如果您使用其他操作系统,您可以下载开源且跨平台的 PowerShell 7。

答案2

这是我的补充。您可以使用此方法实际移动文件。您可以将代码包装在一个函数中,这样您只需调用该函数即可完成工作。

function MovUniMp3 {
    param (
     [Parameter(Valuefrompipeline=$true, Mandatory=$true, Position=0)] $source,
     [Parameter(Valuefrompipeline=$true, Mandatory=$true, Position=1)] $destination
    )

    if (!(Test-Path $destination)) {New-Item -path $destination -itemtype "directory" | Out-Null}
    $file={Get-ChildItem -path $_ -file -filter '*.mp3'}
    $folders=(Get-ChildItem -path $source -directory -force -recurse).fullname | where {$(Invoke-Command $file).count -eq 1}
    $folders | %{Move-Item -path $(Invoke-Command $file).fullname -destination $destination}
}

将代码粘贴到正在运行的 PowerShell 会话中,然后可以通过键入以下内容来使用它MovUniMp3

使用示例:

MoveUniMp3 "C:\Music" "C:\Dest"

这会将 C:\Music 目录内的子文件夹中包含的所有 .mp3 文件(仅一个 .mp3 文件)移动到位于 C:\Dest 的目标文件夹。

相关内容