07-21-2023, 08:28 PM
I have a share that is a "junk drawer" for end-users. They are able to create folders and subfolders as they see fit. I need to implement a script to delete files created more than 31 days old.
I have that started with Powershell. I need to follow up the file deletion script by deleting subfolders that are now empty. Because of the nesting of subfolders, I need to avoid deleting a subfolder that is empty of files, but has a subfolder below it that contains a file.
For example:
- `FILE3a` is 10 days old. `FILE3` is 45 days old.
- I want to clean up the structure removing files older than 30 days, and delete empty subfolders.
<!-- language: none -->
C:\Junk\subfolder1a\subfolder2a\FILE3a
C:\Junk\subfolder1a\subfolder2a\subfolder3a
C:\Junk\subfolder1a\subfolder2B\FILE3b
Desired result:
- Delete: `FILE3b`, `subfolder2B` & `subfolder3a`.
- Leave: `subfolder1a`, `subfolder2a`, and `FILE3a`.
I can recursively clean up the files. How do I clean up the subfolders without deleting `subfolder1a`? (The "Junk" folder will always remain.)
I have that started with Powershell. I need to follow up the file deletion script by deleting subfolders that are now empty. Because of the nesting of subfolders, I need to avoid deleting a subfolder that is empty of files, but has a subfolder below it that contains a file.
For example:
- `FILE3a` is 10 days old. `FILE3` is 45 days old.
- I want to clean up the structure removing files older than 30 days, and delete empty subfolders.
<!-- language: none -->
C:\Junk\subfolder1a\subfolder2a\FILE3a
C:\Junk\subfolder1a\subfolder2a\subfolder3a
C:\Junk\subfolder1a\subfolder2B\FILE3b
Desired result:
- Delete: `FILE3b`, `subfolder2B` & `subfolder3a`.
- Leave: `subfolder1a`, `subfolder2a`, and `FILE3a`.
I can recursively clean up the files. How do I clean up the subfolders without deleting `subfolder1a`? (The "Junk" folder will always remain.)