简体   繁体   English

获取最新文件并将其文件名与文件的rest进行比较

[英]Get the latest file and compare its filename with the rest of the files

I want to get the latest file in a directory and use its filename without the (Count) to check if it has same names with the rest of the files.我想获取目录中的最新文件并使用不带(Count)的文件名来检查它是否与文件的 rest 具有相同的名称。

If filename matches, Rename it as $(Get-Date -Format yyyymmddhhmmss)_$ScreenTitle and move it to Output folder.如果文件名匹配,将其重命名为 $(Get-Date -Format yyyymmddhhmmss)_$ScreenTitle 并将其移动到 Output 文件夹。

If not, Do nothing.如果没有,什么也不做。

EDIT:编辑:

These are the default filenames from the Xbox Gamebar Screenshot (Win+Alt+PrtSc):这些是来自 Xbox Gamebar 屏幕截图 (Win+Alt+PrtSc) 的默认文件名:

在此处输入图像描述

The code below works properly, (Thanks @Theo,) but I've figured-out that filename's time changes.下面的代码可以正常工作,(感谢@Theo,)但我发现文件名的时间发生了变化。 so it does not match the regex.所以它与正则表达式不匹配。

$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)
    }
}

It is a bit unclear if you want to use the date that is part of the file name in the moved file or the curent date.有点不清楚是要使用移动文件中文件名的日期还是当前日期。 You also do not say in what format that date should be when using it in the new file name.在新文件名中使用该日期时,您也没有说明该日期应采用何种格式。

See the comments in the code below for alternatives on the date有关日期的替代方案,请参阅下面代码中的注释

$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
    }
}

As per your comment, below an updated answer:根据您的评论,在更新的答案下方:

$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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM