Recursively Delete File Types From A Named Folder Only

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pmc181
Posts: 1
Joined: 10 Jun 2020 14:03

Recursively Delete File Types From A Named Folder Only

#1 Post by pmc181 » 10 Jun 2020 14:20

I have a weekly cleanup routine I want to setup where I scan an entire directory of users folders and delete file types *.psr and *.zip but only in certain folders. Not every sub-folder.

Each user's folder in this directory has a sub-folder named the exact same name for all users. I only want to delete these files in the named sub-folder of each user. In the screen shot example below it would be the Target-Folder. The Target-Folder name exists for all users.



I came up with this Del statement to run and it works but deletes those file types from all sub-folders and not just the Target-Folder for each user.

DEL /S /Q *.psr *.zip > Deleted-psr-zip.txt 2>&1

This is a network file share server so I can't run powershell. I would like to use cmd's from a batch file.

Thanks,
Pmc181
Attachments
RemoveFiles.jpg
RemoveFiles.jpg (27.52 KiB) Viewed 369 times

Squashman
Expert
Posts: 4160
Joined: 23 Dec 2011 13:59

Re: Recursively Delete File Types From A Named Folder Only

#2 Post by Squashman » 11 Jun 2020 22:56

pmc181 wrote:
10 Jun 2020 14:20
I
This is a network file share server so I can't run powershell. I would like to use cmd's from a batch file.

Thanks,
Pmc181
That is fine if you want to use a batch file but it being a network file share does not rule out Powershell. In fact it probably makes the task easier.

pieh-ejdsch
Posts: 227
Joined: 04 Mar 2014 11:14
Location: germany

Re: Recursively Delete File Types From A Named Folder Only

#3 Post by pieh-ejdsch » 14 Jun 2020 05:07

you can use a for /r loop in a recursive search

Code: Select all

( for /d /r %%a in (Target-Folder.?) do @ DEL /S /Q "%%a\*.psr" "%%a\*.zip" ) >Deleted-psr-zip.txt 2>&1
Phil

Post Reply