简体   繁体   中英

Powershell - Delete Folder Contents With Exclusions

I am trying to find a way to get a list of folders in a specific directory. The directories under the parent folder may contain subfolders themselves. I would like to sort the folders that are directly under the parent folder based on the oldest last access time of any file in any of the subfolders of the parent folder. Once I have the list of folders sorted correctly, I would like to begin deleting the files within the subfolders with a couple of exclusions. There are a some files in the subfolders that I do not want deleted. I would also like to keep a running count of the total size of files that have been deleted. Once I reach a certain threshold with regards to the total size of files removed, I would like to exit out of the script. Thank you.

This could be a starting point but your criterias on what to keep are quite unclear

$MyRootFolderPath = "Your entry folder path"

Get-ChildItem -Path $MyRootFolderPath -Recurse | Where-Object {-not $_.PSIsContainer -and ($_.Name -notmatch "Your pattern to keep")} | Sort-Object -Property LastWriteTime | Select-Object -first 10 | Remove-Item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM