我在 powershell 中做一个简短的脚本,应该删除超过 60 天的文本文件,但是我无法继续,因为脚本只删除了一堆中的一个随机文件,我认为原因是它不在一个数组,这就是它没有遍历所有文本文件的原因。
这是整个脚本:
cls
#clearscreen
$ilogs = get-childitem D:\IMPlogs -recurse
#get all items inside IMPlogs folder
$List = $ilogs | where name -like *.txt.4
#filter items having .txt.(num)
#Write-Host ($List |Format-Table| Out-String)
#list all items in the terminal
$Today = (Get-Date -UFormat %s)
#date to seconds
$ilogsD2S = [datetime]($List).LastWriteTime
#get last write time of file and conv to datetime
#}
$conv2s = (Get-Date $ilogsD2S -UFormat %s)
#conv write time to seconds
#$conv2s
#$Today
$datediff = [int](($Today-$conv2s)/(3600*24))
#substracting today's date and files write time and getting only the whole number
$datediff
Write-Host ($List | Format-Table| Out-String)
#$List | Foreach-Object {Write-Host $_}
这是我需要处理的文本文件。(它会自动填充此文件夹中的文本文件)
尝试这样的事情:
Get-ChildItem "c:\temp" -file | where {$_.Name -like '*.txt.?*' -and $_.LastWriteTime -le (Get-Date).AddDays(-60)} | Remove-Item
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句