powershell 获取最新文件并将其文件名与其余文件进行比较

tv6aics1  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(194)

我想获取目录中的最新文件,并使用不带 (计数) 的文件名来检查它是否与其余文件同名。
如果文件名匹配,请将其重命名为$(Get-Date -Format yyyymmddhhmmss)_$ScreenTitle并将其移动到“输出”文件夹。
如果不是,则不执行任何操作。

编辑:

以下是Xbox游戏栏屏幕截图(Win+Alt+PrtSc)中的默认文件名:

下面的代码工作正常,(谢谢@Theo!)但我已经计算出文件名的时间变化,所以它不匹配正则表达式。

$ParentFolder = "$env:USERPROFILE\Videos\Captures"
#Set-Location $ParentFolder

# Create an Output Folder wether It's Existing or Not
New-Item $ParentFolder\Output -Force -ItemType Directory | Out-Null
$OutputFolder = ".\Output"

Get-ChildItem -Path $ParentFolder -Filter '*.png' -File | Group-Object {$_.BaseName.Split("(")[0].TrimEnd()} | 
Where-Object { $_.Count -gt 1 } | ForEach-Object {
    # get the latest file
    $newestFile = $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    $newestFile

    If ($newestFile.BaseName -match '^(.+)\s+(\d{2}_\d{2}_\d{4}\s+\d{2}_\d{2}_\d{2}\s+[ap]m).*$') {
        $screentitle      = $matches[1]
        $dateFromFileName = $matches[2]  # the date from the filename unaltered like '11_21_2022 10_59_21 AM'
        
        $dateToday = Get-Date -Format "yyyymmddhhmmss"

        # create the new filename
        $NewFileName = '{0}_[{1}]{2}' -f $dateToday, $screenTitle, $newestFile.Extension

        # Move the file with a new name to the destination
        Write-Host "Moving file '$($newestFile.Name)' as '$NewFileName'"
        $newestFile | Move-Item -Destination (Join-Path -Path $OutputFolder -ChildPath $NewFileName)
    }
}
n3h0vuf2

n3h0vuf21#

这是有点不清楚,如果你想使用的日期是文件名的一部分,在移动的文件或当前的日期。你也没有说,在什么格式的日期应该在使用它在新的文件名。
请参阅下面代码中的注解,了解日期的替代项

$ParentFolder = 'Z:\WhereTheFilesAre'
$OutputFolder = 'X:\TheSestinationFolder'

Get-ChildItem -Path $ParentFolder -Filter '*.png' -File | Group-Object {$_.BaseName.Split("(")[0].TrimEnd()} | 
Where-Object { $_.Count -gt 1 } | ForEach-Object {
    # get the latest file
    $newestFile = $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    if ($newestFile.BaseName -match '^(.+)\s+(\d{2}_\d{2}_\d{4}\s+\d{2}_\d{2}_\d{2}\s+[ap]m).*$') {
        $screentitle      = $matches[1]
        $dateFromFileName = $matches[2]  # the date from the filename unaltered like '11_21_2022 10_59_21 AM'

        # if you want the date from the file name formatted like 'yyyy_MM_dd HH-mm-ss'
        # $date = [datetime]::ParseExact($dateFromFileName, 'MM_dd_yyyy h_mm_ss tt', [cultureinfo]::InvariantCulture)
        # $dateFromFileName = '{0:yyyy_MM_dd HH-mm-ss}' -f $date

        # create the new filename
        $NewFileName = '{0}_[{1}]{2}' -f $dateFromFileName, $screenTitle, $newestFile.Extension

        # Move the file with a new name to the destination
        Write-Host "Moving file '$($newestFile.Name)' as '$NewFileName'"
        $newestFile | Move-Item -Destination (Join-Path -Path $OutputFolder -ChildPath $NewFileName)

        # what do you want to do with the rest of the files that were older? Delete them?
        # $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 | Remove-Item
    }
}

根据您的意见,下面是更新后的答案:

$ParentFolder = 'Z:\WhereTheFilesAre'
$OutputFolder = 'X:\TheSestinationFolder'

Get-ChildItem -Path $ParentFolder -Filter '*.png' -File | 
Where-Object { $_.BaseName -match '^(.+)\s+(\d{2}_\d{2}_\d{4}\s+\d{2}_\d{2}_\d{2}\s+[ap]m).*$' } |  # filter more specific
Group-Object { $_.BaseName -replace '\s+\d{2}_\d{2}_\d{4}\s+\d{2}_\d{2}_\d{2}\s+[ap]m.*$' } |       # group on screen title
Where-Object { $_.Count -gt 1 } | ForEach-Object {
    # get the latest file
    $newestFile   = $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    $screentitle  = $_.Name

    # scenario 1: Take the date from the file name
    $dateFromFileName = $newestFile.BaseName -replace '^.+\s+(\d{2}_\d{2}_\d{4}\s+\d{2}_\d{2}_\d{2}\s+[ap]m).*$', '$1'
    # parse the date from the file name
    $date = [datetime]::ParseExact($dateFromFileName, 'MM_dd_yyyy h_mm_ss tt', [cultureinfo]::InvariantCulture)
    # create the new filename
    $NewFileName = '{0:yyyyMMddHHmmss}_[{1}]{2}' -f $date, $screenTitle, $newestFile.Extension

    # scenario 2: Use the LastWriteTime property from the newest file (much easier)
    # $NewFileName = '{0:yyyyMMddHHmmss}_[{1}]{2}' -f $newestFile.LastWriteTime, $screenTitle, $newestFile.Extension

    # Move the file with a new name to the destination
    Write-Host "Moving file '$($newestFile.Name)' as '$NewFileName'"
    $newestFile | Move-Item -Destination (Join-Path -Path $OutputFolder -ChildPath $NewFileName)

    # what do you want to do with the rest of the files that were older? Delete them?
    # $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 | Remove-Item
}

相关问题