我在服务器上有一条路径,每天以YYYYMMDD格式创建FILES。 (20120618.TXT等)
示例文件 APP20161009.TXT APP20161008.TXT APP20161110.TXT
$YourDirectory = "C:\Temp\"
$Threshold = ((get-date).adddays(-2))
ForEach ($FileName in $CleanupList) {
$date = ([datetime]::ParseExact((($FileName.Name.TrimEnd(".txt")).Substring($FileName.Name.Length - 12)), "yyyyMMdd", $null))
if ($date -lt $Threshold) {
Remove-Item $Filename.FullName -Force
}
}
答案 0 :(得分:0)
目标似乎不是删除目录或文件。以下代码假设自" .txt"以来的文件。例子表明。
[cmdletbinding()]
Param()
$YourDirectory = ".\t"
$Threshold = (get-date).adddays(-0)
$CleanupList = Get-ChildItem -File $YourDirectory |
Where-Object {($_.Name -match 'APP\d{8}.txt') -and ($_.LastWriteTime -lt $Threshold)}
if ($CleanupList -ne $null) {
ForEach ($FileName in $CleanupList) {
Write-Verbose $FileName
Write-Verbose $FileName.FullName
Remove-Item -WhatIf $FileName.FullName -Force
}
}