2

There's a need to do a weekly cleanup of FTP user account mailboxes on our server and it has about 50 user accounts defined. Below is an example of the FTP user mailbox folder structure which each include 5 subfolders or so with one in ALL being named of them being named "archive".

I'm trying to figure out a way to delete the contents of the subfolders on a weekly basis; however, I would like to exclude deleting anything from ANY of the "archive" named subfolders (keeping their contents in place) in any of the user account FTP mailboxes.

FTP MAILBOX FOLDER STRUCTURE EXAMPLE

C:\ftp\ftp-users  --> Root Level
         \jmartin --> Mailbox User Account Root Level
            \jmartin-a1 --> Mailbox subfolder
            \jmartin-a2 --> Mailbox subfolder
            \jmartin-a3 --> Mailbox subfolder
            \jmartin-a4 --> Mailbox subfolder
            \jmartin-a5 --> Mailbox subfolder
            \archive    --> DO NOT TOUCH --> Mailbox subfolder
  • 1
    What did you try out yourself? You cant expect people to write a complete script for you without your own input. – Smeerpijp Jan 7 '16 at 9:10
  • Welcome to Super User. We are not a script writing service. We expect users to tell us what they have tried so far (including any scripts they are using) and where they're stuck so that we can help with specific problems. Questions that only ask for scripts are too broad and are likely to be put on hold or closed. Please read How do I ask a good question?. – DavidPostill Jan 7 '16 at 13:30
1

Powershell script to clean up folders

I'm trying to figure out a way to delete the contents of the subfolders on a weekly basis; however, I would like to exclude deleting anything from ANY of the "archive" named subfolders (keeping their contents in place) in any of the user account FTP mailboxes.

You can complete this with simple PowerShell script logic with one of the four I provided you below that use asterisk characters for wildcards of files and/or folders.

The example logic below uses Remove-Item including the -Recurse and -Exclude parameters.

I think the topmost example (or the one just beneath it) is the one you need based on your description, but test with all if needed to confirm. I tested and confirmed that these work just fine, and as expected from a test folder in my environment.

I would suggest that you test these in your environment just to confirm you get it to work just as expected for your needs before you run any against production folders just in case.


PowerShell Command Examples

(Use the below to delete all files with a file extension beneath C:\ftp\ftp-users\*\*\~ excluding ANY subfolders named "Archive" and its contents of the FTP mailboxes at this level)

Remove-Item "C:\ftp\ftp-users\*\*\*.*" -Recurse -Exclude "Archive" -Force

(Use the below to delete all files and all subfolders beneath C:\ftp\ftp-users\*\*\~ excluding ANY subfolder named "Archive" and it's contents of the FTP mailboxes at this level)

Remove-Item "C:\ftp\ftp-users\*\*\*" -Recurse -Exclude "Archive" -Force

(Use the below to delete all files and all subfolders beneath C:\ftp\ftp-users\*\~ excluding the subfolder named "Archive" and it's contents of the FTP mailboxes at this level)

Remove-Item "C:\ftp\ftp-users\*\*" -Recurse -Exclude "Archive" -Force

(Use the below to delete all files with a file extension beneathC:\ftp\ftp-users\*\~ and nothing more so it'll not touch ANY subfolders of the FTP mailboxes at this level so no -Exclude parameter)

Remove-Item "C:\ftp\ftp-users\*\*.*" -Recurse -Force
  • @damarado Did you get a chance to look over my answer above yet? Just curious on your feedback, etc. to start if this will suffice for the need. Also wanted to mention and provide the Accepting an Answer link in case you're not familiar with that process on this site. Let me know whether or not my answer helps you any at all or if you're looking for something further I need to adjust. – Pimp Juice IT Jan 11 '16 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.