【问题标题】:Split files in to subfolders by song duration按歌曲时长将文件拆分到子文件夹
【发布时间】:2018-03-19 21:03:21
【问题描述】:

我对 PowerShell 完全陌生,通过四处搜索我没有找到任何关于它的信息。所以我会在这里询问是否有人知道这是否可能以及如何做到这一点......

我的文件夹中有 1000 个 mp3 文件。而且我想将它们分成子文件夹,但每个子文件夹的播放时间应为 1 小时,或者至少尽可能接近 1 小时(最好超过 1 小时,而不是更少)。

所以我必须查看 mp3 文件的长度(它们通常长 2-4 分钟)。我想首先获得足够的 mp3 文件,以便总共播放 1 小时。然后将它们移动到一个子文件夹中,并将其命名为List *n*,其中 n 为每个子文件夹递增。喜欢:列表 1、列表 2、列表 3

这可能吗?

【问题讨论】:

标签: shell powershell foreach directory subdirectory


【解决方案1】:

更新:这是工作脚本!设法解决它! :) 编辑播放列表路径(主目录,将在其中创建所有子文件夹)。同时编辑播放列表长度!

Function Get-MP3Data
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([Psobject])]
    Param
    (
        [String] [Parameter(Mandatory=$true, ValueFromPipeline=$true)] $Directory
    )

    Begin
    {
        $shell = New-Object -ComObject "Shell.Application"
    }
    Process
    {

        Foreach($Dir in $Directory)
        {
            $ObjDir = $shell.NameSpace($Dir)
            $Files = gci $Dir| ?{$_.Extension -in '.mp3','.mp4'}

            Foreach($File in $Files)
            {
                $ObjFile = $ObjDir.parsename($File.Name)
                $MetaData = @{}
                $MP3 = ($ObjDir.Items()|?{$_.path -like "*.mp3" -or $_.path -like "*.mp4"})
                $PropertArray = 0,1,2,12,13,14,15,16,17,18,19,20,21,22,27,28,36,220,223

                Foreach($item in $PropertArray)
                { 
                    If($ObjDir.GetDetailsOf($ObjFile, $item)) #To avoid empty values
                    {
                        $MetaData[$($ObjDir.GetDetailsOf($MP3,$item))] = $ObjDir.GetDetailsOf($ObjFile, $item)
                    }

                }

                New-Object psobject -Property $MetaData |select *, @{n="Directory";e={$Dir}}, @{n="Fullname";e={Join-Path $Dir $File.Name -Resolve}}, @{n="Extension";e={$File.Extension}}
            }
        }
    }
    End
    {
    }
}

# Create playlist
$TotalLength = 0
$MaxLength = 3600
$TempPlaylist = @()
$PlaylistName = 1
$PlaylistPath = "C:\Users\Admin\Desktop\New folder"

if ($TotalLength -lt $MaxLength)
{   
    ForEach($item in ($PlaylistPath |Get-Mp3Data)){

        # Get all song names and song durations
        $SongName = $item.Fullname
        $Seconds = [int](([datetime]$item.Length).TimeOfDay.TotalSeconds)

        # Append song duration to total length and add song to temporary playlist
        $TotalLength += $Seconds
        $TempPlaylist += ,$SongName

        # Create a folder for the playlist if it doesn't exist
        if (!(test-path $PlaylistPath\$PlaylistName))
        {
            New-item -ItemType Directory -Force -Name $PlaylistName -Path $PlaylistPath
            Write-Host Created new folder $PlaylistName
        }

        # Check if the total length is >= maxlength, then move temporary playlist to a new folder
        if ($TotalLength -ge $MaxLength)
        {   
            # Loop through all songs in the temporary playlist
            For($i=0; $i -lt $TempPlaylist.length; $i++)
            {
                Move-Item -Path $TempPlaylist[$i] -Destination $PlaylistName
                Write-Host Moved file $TempPlaylist[$i] to $PlaylistName
            }

            # Increment playlist folder name
            $PlaylistName++

            # Reset the temporary playlist
            $TempPlaylist = @()

            # Reset the temporary playlist length
            $TotalLength = 0
        }
    }
}

要为其添加随机化(以便 mp3 文件在插入播放列表之前随机排序),只需更改

ForEach($item in ($PlaylistPath | Get-Mp3Data)){

ForEach($item in ($PlaylistPath | Get-Mp3Data) | Sort-Object {Get-Random}){

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 2022-06-11
    • 2014-08-14
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2017-05-20
    • 2021-09-13
    相关资源
    最近更新 更多