Powershell脚本删除最旧的文件(不相对于当前date)

我不想“删除X天以前的文件”。 如果我想这样做,我只是从那里已经写了成千上万的脚本之一,我不会问这样一个微不足道的问题在ServerFault上。

我想要删除特定文件夹中除最近N个文件以外的所有文件。

背景:将远程系统configuration为将定期存档文件转储到Windows服务器上的共享。 这些文件的命名约定为YYYYMMDD-hhmm.ext。 如果存档过程失败,我不希望清除脚本删除文件,只是因为他们比那么多天更旧。 无法将远程系统configuration为在归档过程中清除文件。

例如,脚本运行时可能有10个文件,可能有7个文件,文件夹中可能有5个文件。 无论年龄多大,我都需要至less保留最近的5次。

这可以用PowerShell完成吗?

编辑:这将是可取的忽略不符合命名约定的文件或文件夹。 其他文件和文件夹不应该被删除,也不应该混淆脚本(导致保留less于5个档案文件)。

用法:yourScript.ps1 -Pathpath\到\运行\对[-NumberToSave N]

param([String]$Path,[Int32]$NumberToSave=5) $items = Get-ChildItem "$Path\*.ext" | Where-Object Name -match "\d\d\d\d\d\d\d\d-\d\d\d\d\.ext" | Sort-Object Name -Descending if ($NumberToSave -lt $items.Count) { $items[$NumberToSave..($items.Count-1)] | Remove-Item } 

我不认为这真的比@John更好,我只是做了这个尝试参数,正则expression式,而不是使用循环。 做一个正则expression式匹配确实有排除其他与.ext格式不匹配的文件(例如20130504-1200.txt.ext,20.ext)的好处,但这是可疑的。

我很失望,如果$ NumberToSave = $ items.Count那么if语句是必要的,否则它不会在那里。 技术上:

 $items[$numberToSave..$items.Count] | Remove-Item 

工作正常(PowerShell似乎没有错误,如果你引用过去的数组边界,它只是处理和忽略它?),但似乎有可能混淆。

 $n = x $items = Get-ChildItem path_to_folder\*.ext $items | Sort-Object Name -Descending | Select-Object -Last ($items.count - $n) | Foreach-Object { Remove-Item $_ } 

一位朋友提供了这个脚本:

 # Defines how many files you want to keep? $Keep = 5 # Specifies file mask $FileMask = "*.ext" # Defines base directory path $Path = "F:\path\to\backups\" # Creates a full path plus file mask value $FullPath = $Path + $FileMask # Creates an array of all files of a file type within a given folder, reverse sort. $allFiles = @(Get-ChildItem $FullPath) | SORT Name -descending # Checks to see if there is even $Keep files of the given type in the directory. If ($allFiles.count -gt $Keep) { # Creates a new array that specifies the files to delete, a bit ugly but concise. $DeleteFiles = $allFiles[$($allFiles.Count - ($allFiles.Count - $Keep))..$allFiles.Count] # ForEach loop that goes through the DeleteFile array ForEach ($DeleteFile in $DeleteFiles) { # Creates a full path and delete file value $dFile = $Path + $DeleteFile.Name # Deletes the specified file Remove-Item $dFile } } 
 # Get all the items in `C:\Archive` Get-ChildItem -Path 'C:\Archive' | # Skip directories Where-Object { -not $_.PsIsContainer } | # Only get files that match YYYYMMDD-HHMM.ext format Where-Object { $_.Name -match '\d{8}-\d{4}.ext' } | # Sort them by name, from most recent to oldest Sort-Object -Descending -Property Name | # Keep the first five items Select-Object -Skip 5 | Remove-Item -WhatIf 

我之前在Bash中已经完成了这个工作,您可以将其翻译成PowerShell。 就我个人而言,我是一个Linuxpipe理员,对于Windows / PowerShell自己也不太了解。

 #!/bin/bash # Directory with backups in need of cleaning bkps_dir="/opt/bkps/hosting.madyork.com" # Make sure that there are at least 7 days worth of backups to avoid removing # the only backups left. if [ "$(/bin/find "${bkps_dir}" -maxdepth 1 -type d | /usr/bin/wc -l)" -lt 8 ]; then echo "Less than 8 backups found!" echo "Will not delete anything!" echo "Exiting..." exit 1 fi /bin/find "${bkps_dir}" -maxdepth 1 -type d -mtime +7 -print0 | /usr/bin/xargs -0 /bin/rm -rf 

对不起,我不能更有帮助。

编辑:我也放在这里,以防万一Linuxpipe理员需要类似的信息。