如何编辑以下 Powershell 代码来重命名 MP4 视频文件并附加分辨率?

如何编辑以下 Powershell 代码来重命名 MP4 视频文件并附加分辨率?

有人知道如何编辑我在下面找到的代码,以便文件名末尾显示 1080p 或 720p 吗?我希望我的文件名从此开始

黑豹.2018.1080p.BluRay.x264-[YTS.AM].txt
刺猬索尼克.1920.1080p.WEBRip.x264.AAC-[YTS.MX].txt
迪克和简的乐趣.2005.1080p.BrRip.x264.YIFY.txt                                              
偷天换日.2003.720p.BrRip.x264.YIFY.txt                                                  
泰山传奇.1916.720p.BluRay.x264-[YTS.AG].txt

对此...

黑豹 2018.1080p.txt
刺猬索尼克 1920 1080p.txt
迪克和简的乐趣 2005 1080p.txt                                              
偷天换日 2003 720p.txt                                                  
泰山传奇 1916 720p.txt

这是我正在使用的代码:

Set-Location "X:\AUTOMATED_FILM_&_TV_SHOWS\2_SEPERATING_FOLDER\MOVIES"
Get-ChildItem | Where-Object{$_.BaseName -Match '(.*?\.(19|20)\d{2})\..*'} | Rename-Item -NewName {($_.BaseName -replace '(.*?\.(19|20)\d{2})\..*','$1')+$_.Extension}
dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace("."," ") + $_.Extension }

答案1

我认为这样就可以了:(-WhatIF测试运行后删除)

Set-Location 'X:\AUTOMATED_FILM_&_TV_SHOWS\2_SEPERATING_FOLDER\MOVIES'
Get-ChildItem -Name | ForEach {
    If ( $_ -match '(^.+(?:720p|1080p)).+(\.\w{3})' ) {
        Rename-Item -LiteralPath $_ -NewName ( $matches[1].Replace('.',' ') + $matches[2] ) -WhatIf
    }
}

使用此代码测试名称构造:

'The.Titan.2018.720p.WEBRip.x264-[YTS.AM].mp4',
'Avatar.2018.1080p.WEBRip.x264-[YTS.AM].mp4' | ForEach {
    If ( $_ -match '(^.+(?:720p|1080p)).+(\.\w{3})' ) {
        echo ( $matches[1].Replace('.',' ') + $matches[2] )
    }
}

输出

PS C:\...\keith>ren test.reg atesty.reg^C
PS C:\...\keith>cd \
PS C:\> 'The.Titan.2018.720p.WEBRip.x264-[YTS.AM].mp4',
>> 'Avatar.2018.1080p.WEBRip.x264-[YTS.AM].mp4' | ForEach {
>>     If ( $_ -match '(^.+(?:720p|1080p)).+(\.\w{3})' ) {
>>         echo ( $matches[1].Replace('.',' ') + $matches[2] )
>>     }
>> }
The Titan 2018 720p.mp4
Avatar 2018 1080p.mp4
PS C:\>

答案2

这似乎能起到作用。[咧嘴笑] 它假定所有文件都包含WEBRip文件名,并且该字符串是您想要拆分文件基本名称的地方。注释似乎足够了,但如果任何代码不明显,请随时询问。

$SourceDir = "$env:TEMP\LexPointOh"

#region >>> create files to work with
#    when ready to do this with real data point the "$SourceDir" to your real data
if (-not (Test-Path $SourceDir))
    {
    $Null = mkdir -Path $SourceDir
    }
@(
    'The.Titan.2018.720p.WEBRip.x264-[YTS.AM].mp4'
    'Avatar.2018.1080p.WEBRip.x264-[YTS.AM].mp4'
    ) |
    ForEach-Object {
        $Null = New-Item -Path $SourceDir -Name $_ -ItemType 'File' -ErrorAction 'SilentlyContinue'
        }
#endregion >>> create files to work with

$FileList = Get-ChildItem -LiteralPath $SourceDir -Filter '*.mp4' -File |
    Where-Object {
        # skip files that already end with a resolution value
        $_.BaseName -notmatch '\d{1,}p$'
        }

foreach ($FL_Item in $FileList)
    {
    # match everything from the start of the string up to & including the "<digit><p><dot>"
    #    the "$Null" is there to dump the unwanted output of the match operator
    $Null = $FL_Item.BaseName -match '^.+\dp\.'
    # get the 1st match from the "$Matches" automatic variable
    #    trim away any trailing dot
    #    split on the remaining dots
    #    join the results with spaces
    #    finally, add the file extension
    $NewName = ($Matches[0].TrimEnd('.').Split('.') -join ' ') + $FL_Item.Extension
    $FullNewName = Join-Path -Path $SourceDir -ChildPath $NewName

    if (-not (Test-Path -LiteralPath $FullNewName))
        {
        Rename-Item -LiteralPath $FL_Item.FullName -NewName $NewName #-WhatIf
        }
        else
        {
        Write-Warning ('    A file named [ {0} ] already exists in {1}' -f $NewName, $SourceDir)
        }
    }

Avatar 2018 1080p.mp4当$SourceDir` 中 已经有一个文件时输出...

WARNING:     A file named [ Avatar 2018 1080p.mp4 ] already exists in C:\Temp\LexPointOh

否则没有输出。您可能想要添加此类内容 - 或将失败/成功记录到文件中以供日后使用。

相关内容