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

Powershell每天删除除最新文件之外的所有文件,powershell,delete-file,Powershell,Delete File,我有一个包含很多文件的文件夹,每天有多个文件 我想脚本的东西,删除所有,但最新的文件每天 我见过很多删除超过X天的文件的脚本,但这有点不同,而且在昨天之前没有编写过powershell(我是专门的tsql),我真的不知道该怎么做 我不是要求任何人为我写代码,但也许描述一下实现这一点的方法会很好,我可以开始研究如何将其付诸实践 所有文件都在一个目录中,没有子文件夹。有些文件我不想删除,我想删除的文件的文件名格式为constant\u filename\u prefix\u YYYYMMDDHHMM

我有一个包含很多文件的文件夹,每天有多个文件

我想脚本的东西,删除所有,但最新的文件每天

我见过很多删除超过X天的文件的脚本,但这有点不同,而且在昨天之前没有编写过powershell(我是专门的tsql),我真的不知道该怎么做

我不是要求任何人为我写代码,但也许描述一下实现这一点的方法会很好,我可以开始研究如何将其付诸实践

所有文件都在一个目录中,没有子文件夹。有些文件我不想删除,我想删除的文件的文件名格式为
constant\u filename\u prefix\u 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
在这两个备选方案中,您会发现在
删除项
cmdlet的末尾有一个开关
-WhatIf
。Yhis用于测试代码,不会删除任何文件。相反,使用此开关,它会在控制台中写出将发生的情况

一旦您对此输出感到满意,就可以删除或注释掉
-WhatIf
开关,让代码删除文件


更新

正如我现在所了解的,该文件夹中有多个文件持续数天,您希望保留每天的最新文件,删除其他文件

在这种情况下,我们必须创建文件的“日”组,并使用每个组按日期排序并删除旧文件。
这就是
组对象
的作用

方法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
        }

你试过什么?什么东西没有按预期工作?这个网站主要是帮助现有的代码。。。这是一种“我甚至不知道从哪里开始”的情况啊!嗯,看起来西奥给了你一些奥塔能用的代码。。。[咧嘴笑]我们没有你的消息。。我的回答解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易地找到它。这并不是我想要达到的效果-这将删除除最新文件之外的所有与模式匹配的文件。我想做的是每天保存最新的文件。例如,如果我有三个今天的文件和两个昨天的文件,我需要保留今天的最新文件和昨天的最新文件,只剩下两个文件。这样做的目的是只保留从今天开始的最新信息,只留下一个文件。不过,谢谢你的帮助——每天一次的概念正是让我头疼的原因。@JamesLester是的,我想这就是你想要的。我现在在手机上,但明天会尝试给出另一个答案。您需要什么日期:文件名中的日期还是真实的LastWriteTime?@JamesLester我已经更新了我的答案。它现在每天保留最新的文件,并删除较旧的文件。