簡體   English   中英

Powershell 每天刪除除最新文件之外的所有文件

[英]Powershell Delete all files apart from the latest file per day

我有一個文件夾,里面有很多文件,每天有多個文件。

我想編寫一些腳本,每天刪除除最新文件之外的所有文件。

我見過很多刪除 X 天前文件的腳本,但這略有不同,並且在昨天之前沒有寫過 powershell(我只使用 tsql),我不確定如何去做。

我不是要求任何人為我編寫代碼,但也許描述實現這一目標的方法會很好,我可以研究如何將其付諸實踐。

所有文件都在一個目錄中,沒有子文件夾。 有些文件我不想刪除,我想刪除的文件的文件名格式為constant_filename_prefix_YYYYMMDDHHMMSS.zip

powershell 是正確的工具嗎? 我應該看看 Python(我也不知道)Powershell 更方便,因為我們擁有的其他代碼是用 PS 編寫的。

PowerShell 為此類事情提供了易於使用的 cmdlet。

我的問題是,您是否希望使用文件名中的日期,或者文件本身的實際 LastWriteTime 日期(如文件資源管理器中所示)。

下面有兩種處理方法。 我已經添加了很多代碼注釋來幫助您獲得圖片。

如果要根據實際上次寫入時間刪除文件:

$sourceFolder = 'D:\test'                                             # put the path to the folder where your files are here
$filePrefix   = 'constant_filename_prefix'

Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File |  # get files that start with the prefix and have the extension '.zip'
    Where-Object { $_.BaseName -match '_\d{14}$' } |                  # that end with an underscore followed by 14 digits
    Sort-Object -Property LastWriteTime -Descending |                 # sort on the LastWriteTime property
    Select-Object -Skip 1 |                                           # select them all except for the first (most recent) one
    Remove-Item -Force -WhatIf                                        # delete these files

或者

如果要根據文件名中的日期刪除文件。
因為您使用的日期格式是可排序的,所以您可以安全地對文件 BaseName 的最后 14 位進行排序:

$sourceFolder = 'D:\test' 
$filePrefix   = 'constant_filename_prefix'

Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File |                 # get files that start with the prefix and have the extension '.zip'
    Where-Object { $_.BaseName -match '_\d{14}$' } |                                 # that end with an underscore followed by 14 digits
    Sort-Object -Property @{Expression = {$_.BaseName.Substring(14)}} -Descending |  # sort on the last 14 digits descending
    Select-Object -Skip 1 |                                                          # select them all except for the first (most recent) one
    Remove-Item -Force -WhatIf                                                       # delete these files

在這兩種選擇中,您會發現在Remove-Item cmdlet 的末尾有一個開關-WhatIf Yhis 用於測試代碼,實際上不會刪除任何文件。 相反,使用此開關,它在控制台中寫出發生什么。

一旦您對這個輸出感到滿意,您可以刪除或注釋掉-WhatIf開關以讓代碼刪除文件。


更新

據我所知,該文件夾中有幾天的多個文件,您希望每天保留最新的文件,刪除其他文件。

在這種情況下,我們必須創建文件的“天”組,每個組按日期排序並刪除舊文件。
這就是Group-Object用武之地。

方法 1) 使用文件的 LastWriteTime 屬性

$sourceFolder = 'D:\\test' # put the path to the folder where your files are here $filePrefix = 'constant_filename_prefix' Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File | # get files that start with the prefix and have the extension '.zip' Where-Object { $_.BaseName -match '_\\d{14}$' } | # that end with an underscore followed by 14 digits Group-Object -Property @{Expression = { $_.LastWriteTime.Date }} | # create groups based on the date part without time part ForEach-Object { $_.Group | Sort-Object -Property LastWriteTime -Descending | # sort on the LastWriteTime property Select-Object -Skip 1 | # select them all except for the first (most recent) one Remove-Item -Force -WhatIf # delete these files }

方法 2) 使用取自文件名的日期:

 $sourceFolder = 'D:\\test' # put the path to the folder where your files are here $filePrefix = 'constant_filename_prefix' Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File | # get files that start with the prefix and have the extension '.zip' Where-Object { $_.BaseName -match '_\\d{14}$' } | # that end with an underscore followed by 14 digits Group-Object -Property @{Expression = { ($_.BaseName -split '_')[-1].Substring(0,8)}} | # create groups based on the date part without time part ForEach-Object { $_.Group | Sort-Object -Property @{Expression = {$_.BaseName.Substring(14)}} -Descending | # sort on the last 14 digits descending Select-Object -Skip 1 | # select them all except for the first (most recent) one Remove-Item -Force -WhatIf # delete these files }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM