[英]Powershell script to delete files older than x days, but not folders
[英]Filter and delete files and folders(and files inside of folders) older than x days in powershell
這是我在這個論壇上的第一篇文章。 我是編碼初學者,我需要使用我的第一個自編碼工具之一的幫助。 我制作了一個小腳本,它根據文件是否早於日期 x(lastwritetime)來刪除文件。 現在我的問題是:我希望腳本還檢查目錄內文件夾內的文件,並且只有在文件夾確實為空時才刪除該文件夾。 我不知道如何解決這個問題中的遞歸,似乎腳本只刪除了與日期 x 相關的整個文件夾。 誰能告訴我我在這段代碼中遺漏了什么並幫助我創建自己的遞歸來解決問題或修復代碼? 謝謝大家,伙計們:這是我的代碼:
如果有人知道如何使用 function 使代碼工作,我會很高興
$path = Read-Host "please enter your path"
"
"
$timedel = Read-Host "Enter days in the past (e.g -12)"
$dateedit = (Get-Date).AddDays($timedel)
"
"
Get-ChildItem $path -File -Recurse | foreach{ if ($_.LastWriteTime -and !$_.LastAccessTimeUtc -le $dateedit) {
Write-Output "older as $timedel days: ($_)" } }
"
"
pause
Get-ChildItem -Path $path -Force -Recurse | Where-Object { $_.PsisContainer -and $_.LastWriteTime -le $dateedit } | Remove-Item -Force -Recurse
""
Write-Output "Files deleted"
如果文件夾是空的,還要刪除比過去設置的日期更早的文件夾,這會給您帶來這樣的問題,即一旦從此類文件夾中刪除文件,文件夾的 LastWriteTime 就會被設置為該時刻。
這意味着您應該先獲取舊文件夾列表,然后再開始刪除舊文件,然后使用該列表刪除這些文件夾(如果它們為空)。
此外,應該對來自 Read-Host 的用戶輸入進行最低限度的檢查。 (即路徑必須存在並且天數必須可轉換為 integer 數字。對於后者,我選擇簡單地將其轉換為[int]
因為如果失敗,代碼無論如何都會生成一個執行。
嘗試類似的東西
$path = Read-Host "please enter your path"
# test the user input
if (-not (Test-Path -Path $path -PathType Container)) {
Write-Error "The path $path does not exist!"
}
else {
$timedel = Read-Host "Enter days in the past (e.g -12)"
# convert to int and make sure it is a negative value
$timedel = -[Math]::Abs([int]$timedel)
$dateedit = (Get-Date).AddDays($timedel).Date # .Date sets this date to midnight (00:00:00)
# get a list of all folders (FullNames only)that have a LastWriteTime older than the set date.
# we check this list later to see if any of the folders are empty and if so, delete them.
$folders = (Get-ChildItem -Path $path -Directory -Recurse | Where-Object { $_.LastWriteTime -le $dateedit }).FullName
# get a list of files to remove
Get-ChildItem -Path $path -File -Recurse | Where-Object { $_.LastWriteTime -le $dateedit} | ForEach-Object {
Write-Host "older as $timedel days: $($_.FullName)"
$_ | Remove-Item -Force -WhatIf # see below about the -WhatIf safety switch
}
# now that old files are gone, test the folder list we got earlier and remove any if empty
$folders | ForEach-Object {
if ((Get-ChildItem -Path $_ -Force).Count -eq 0) {
Write-Host "Deleting empty folder: $_"
$_ | Remove-Item -Force -WhatIf # see below about the -WhatIf safety switch
}
}
Write-Host "All Done!" -ForegroundColor Green
}
Remove-Item 上使用的-WhatIf
開關是為了您自己的安全。 這樣,實際上不會刪除任何文件或文件夾,而是在控制台中寫入將要刪除的內容。 如果您對這一切都滿意,請刪除-WhatIf
並再次運行代碼以真正刪除文件和文件夾
param(
[IO.DirectoryInfo]$targetTolder = "d:\tmp",
[DateTime]$dateTimeX = "2020-11-15 00:00:00"
)
Get-ChildItem $targetTolder -Directory -Recurse | Sort-Object {$_.FullName} -Descending | ForEach-Object {
Get-ChildItem $_ -File | Where-Object {$_.LastWriteTime -lt $dateTimeX} | Remove-Item -Force
if ((Get-ChildItem $_).Count -eq 0){Remove-Item $_ -Force}
}
測試后刪除 -WhatIf
嘗試這樣的事情:
$timedel=-12
#remove old files
Get-ChildItem "C:\temp" -Recurse -File | Where LastWriteTime -lt (Get-Date).AddDays($timedel) | Remove-Item -Force
#remove directory without file
Get-ChildItem "C:\temp\" -Recurse -Directory | where {(Get-ChildItem $_.FullName -Recurse -File).count -eq 0} | Remove-Item -Force -recurse
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.