Powershell 在数组中隔离相似的文件名以进行其他处理

Powershell 在数组中隔离相似的文件名以进行其他处理,powershell,file,sorting,Powershell,File,Sorting,我有一个按分辨率对视频文件进行排序的小脚本。但是,有些文件具有相同的视频,但分辨率不同。它们的名称相同,只是其中一个文件名后多了两个字符。i、 e.流12345.ts和流12345_2.ts。我想在数组中找到这些类似的文件,并将它们处理到一个单独的文件夹“Duplicate-”+$MetaMediaFrameHight中,但无法确定在脚本中的什么位置或执行此操作的逻辑 $VideoExtensions = ('.mp4','.ts') $Files = Get-ChildItem -Litera

我有一个按分辨率对视频文件进行排序的小脚本。但是,有些文件具有相同的视频,但分辨率不同。它们的名称相同,只是其中一个文件名后多了两个字符。i、 e.流12345.ts和流12345_2.ts。我想在数组中找到这些类似的文件,并将它们处理到一个单独的文件夹“Duplicate-”+$MetaMediaFrameHight中,但无法确定在脚本中的什么位置或执行此操作的逻辑

$VideoExtensions = ('.mp4','.ts')
$Files = Get-ChildItem -LiteralPath $PSScriptRoot | Where-Object {$_.Extension -in $VideoExtensions}
FOREACH ($file in $Files)
{
  $Shell = New-Object -COMObject Shell.Application
  $Folder = Split-Path $file.FullName
  $ShellFolder = $Shell.NameSpace($Folder)
  $ShellFile = $ShellFolder.ParseName($file)

  $METAMediaFrameHieght = $ShellFolder.GetDetailsOf($ShellFile, 314)

  New-Item $PSScriptRoot\$METAMediaFrameHieght -Type Directory -Force | Out-Null
  Move-Item -LiteralPath $file.FullName -Destination $PSScriptRoot\$METAMediaFrameHieght
}

您可以使用正则表达式过滤器捕获具有
\u number.ts
\u number.mp4
的所有文件

$Files = Get-ChildItem -LiteralPath $PSScriptRoot | Where-Object {$_.Extension -in $VideoExtensions}

$FilesToMove = $Files | ? {$_.Name -match "(_\d*.(ts|mp4)$)" }

if ($FilesToMove){
    $FilesToMove | Move-Item -Path $_.FullName -Destination destPath 
}

您可以使用正则表达式过滤器捕获具有
\u number.ts
\u number.mp4
的所有文件

$Files = Get-ChildItem -LiteralPath $PSScriptRoot | Where-Object {$_.Extension -in $VideoExtensions}

$FilesToMove = $Files | ? {$_.Name -match "(_\d*.(ts|mp4)$)" }

if ($FilesToMove){
    $FilesToMove | Move-Item -Path $_.FullName -Destination destPath 
}

您的代码几乎就在那里,除了解析出BaseName末尾带有
\u number
的文件之外,之后没有清理来清除内存中的COM对象

这应该满足您的要求:

$Shell = New-Object -COMObject Shell.Application
$VideoExtensions = ('.mp4','.ts')
Get-ChildItem -LiteralPath $PSScriptRoot | 
    Where-Object {$_.Extension -in $VideoExtensions -and $_.BaseName -match '_\d+$'} |
    ForEach-Object {
        $ShellFolder = $Shell.NameSpace($_.DirectoryName)
        $ShellFile   = $ShellFolder.ParseName($_.Name)
        $frameHeight = $ShellFolder.GetDetailsOf($ShellFile, 314)
        $destination = Join-Path -Path $PSScriptRoot -ChildPath $frameHeight
        # test if the destination path exists and if not, create it
        if (!(Test-Path -Path $destination -PathType Container)) {
            $null = New-Item -Path $destination -ItemType Directory -Force
        }
        # move the file
        $_ | Move-Item -Destination $destination -WhatIf
}

# Clean up COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ShellFile)   | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ShellFolder) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Shell)       | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

当您对控制台中显示的信息感到满意时,您可以删除
-WhatIf
开关以实际移动文件。

您的代码几乎就在那里,除了解析出在BaseName末尾带有
\u number
的文件之外,之后没有清理来清除内存中的COM对象

这应该满足您的要求:

$Shell = New-Object -COMObject Shell.Application
$VideoExtensions = ('.mp4','.ts')
Get-ChildItem -LiteralPath $PSScriptRoot | 
    Where-Object {$_.Extension -in $VideoExtensions -and $_.BaseName -match '_\d+$'} |
    ForEach-Object {
        $ShellFolder = $Shell.NameSpace($_.DirectoryName)
        $ShellFile   = $ShellFolder.ParseName($_.Name)
        $frameHeight = $ShellFolder.GetDetailsOf($ShellFile, 314)
        $destination = Join-Path -Path $PSScriptRoot -ChildPath $frameHeight
        # test if the destination path exists and if not, create it
        if (!(Test-Path -Path $destination -PathType Container)) {
            $null = New-Item -Path $destination -ItemType Directory -Force
        }
        # move the file
        $_ | Move-Item -Destination $destination -WhatIf
}

# Clean up COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ShellFile)   | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ShellFolder) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Shell)       | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

当您对控制台中显示的信息感到满意时,可以删除
-WhatIf
开关以实际移动文件。

我们没有收到您的消息。。给出的答案解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易地找到它。我们没有收到你的来信。。给出的答案解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易地找到它。