【问题标题】:Get the latest file and compare its filename with the rest of the files获取最新文件并将其文件名与其余文件进行比较
【发布时间】:2022-11-23 17:01:25
【问题描述】:

我想获取目录中的最新文件并使用其文件名而不带(数数)检查它是否与其余文件具有相同的名称。

如果文件名匹配,将其重命名为 $(Get-Date -Format yyyymmddhhmmss)_$ScreenTitle 并将其移至输出文件夹。

如果没有,什么也不做。

编辑:

这些是来自 Xbox Game Bar 屏幕截图 (Win+Alt+PrtScn) 的默认文件名:

下面的代码工作正常,(感谢@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)
    }
}

【问题讨论】:

    标签: powershell filenames get-childitem


    【解决方案1】:

    有点不清楚是要使用移动文件中文件名的日期还是当前日期。 在新文件名中使用该日期时,您也没有说明该日期应采用何种格式。

    有关日期的替代方案,请参阅下面代码中的 cmets

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

    【讨论】:

    • 你好! @Theo,再次感谢这些 :) 我运行了您的代码,一旦它按我喜欢的方式运行,但在重新启动 ISE 后,代码不再继续匹配正则表达式。会出什么问题?
    • @jeriru 很难说.. Parentfolder 中是否有任何文件,如果有,它们的格式是否与您提供的示例相同?你能给出不再匹配的文件名的新例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多