Script to delete files

User avatar
PaulB
SilverLounger
Posts: 1610
Joined: 26 Jan 2010, 20:28
Location: Ottawa ON

Script to delete files

Post by PaulB »

I have a number of folders containing files with different extensions. I want to delete all files with an extension of .xyz but as there are so many occurrences, I don't want to do this manually. My scripting skills are next to zero. Can anyone lend me a hand at automating the process?

Thanks.
Regards,
Paul

The pessimist complains about the wind. The optimist expects it to change. The realist adjusts his sails.

User avatar
Jay Freedman
Microsoft MVP
Posts: 1321
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Script to delete files

Post by Jay Freedman »

You don't really need scripting for this. A single command will do it.

Open a command window (it may be listed on your Start menu, or you can press Windows key + R to get the Run box and type cmd and press Enter).

If all the folders you want to work on are subfolders of one main folder, enter a CD command to go to that main folder. You can start at C:\ if you aren't sure.

Enter the command DEL /S *.xyz and press Enter. The /S switch says to look in all the subfolders of the current folder, and subfolders of those folders, all the way down.

If you want to be prompted for Yes or No for each file, include a /P switch before or after the /S switch.

If you'd prefer to see a list of the files that could be deleted, without actually deleting them yet, there's a way to do that in PowerShell.

User avatar
PaulB
SilverLounger
Posts: 1610
Joined: 26 Jan 2010, 20:28
Location: Ottawa ON

Re: Script to delete files

Post by PaulB »

Thanks Jay. I didn't think it would be as simple as that. I'll give it a try.

If this becomes a regular thing, could I save this as a script to be used repetitively?
Regards,
Paul

The pessimist complains about the wind. The optimist expects it to change. The realist adjusts his sails.

User avatar
Jay Freedman
Microsoft MVP
Posts: 1321
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Script to delete files

Post by Jay Freedman »

Yes, paste it into an empty text file and save it with a .bat extension -- that makes it a "batch file", which is executable just by double-clicking it.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Script to delete files

Post by Rudi »

Hi Paul,

Windows Explorer

A few other "non-script" options...

You can use Windows Explorer to do what you need. (IMHO, Win Explorer is actually very underrated!)

If you navigate to the highest directory in which the files are that you want to delete
Then in the search box, just type the *.xyz extension
This will list all of these file types from all sub directories in the directory you started at
Once the list of file types is displayed, you can select and delete them

See the screen shots below:
I navigated to SAMPLE FILES. You will see it lists multiple sub directories
Image 1.png
Now in the search box (top right) I type out (for demo) the filter *.*
This lists ALL files in all the directories inside SAMPLE FILES
(Note: This step is not necessary - only for demo)
Image 2.png
The next screen shot shows how I used the filter *.doc to extract all Word files.
Once the Word files from all the SAMPLE FILES sun folders are listed, I can simply select them and delete them (or copy them, or move them, etc)
Obviously in your case, you type the extension you need: Like the *.xyz as you referenced.
Image 3.png
You do not have the required permissions to view the files attached to this post.
Last edited by Rudi on 01 Dec 2015, 04:29, edited 1 time in total.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Script to delete files

Post by Rudi »

File Bucket

Another useful third party app to do this type of thing is called File Bucket which is portable software, so no installation needed.

File Bucket is used to perform batch operations on a list of files that you browse to.
You can collect multiple files from different locations (hence the name "File Bucket") or you can navigate to a single main directory and run file operations on a list of files inside that folder (incl. the ability to filter for specific file types) as shown in the screen shot below...

I navigated to the same SAMPLE FILES location
Then added a "docx" filter (seen on the middle right of image)
And at the bottom I selected the operation to delete items to the Recycle Bin.

I have used this app many times to do some actions all at once on lists of files in completely different locations. A VERY handy tool.
Image 4.png
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
PaulB
SilverLounger
Posts: 1610
Joined: 26 Jan 2010, 20:28
Location: Ottawa ON

Re: Script to delete files

Post by PaulB »

Jay Freedman wrote:Yes, paste it into an empty text file and save it with a .bat extension -- that makes it a "batch file", which is executable just by double-clicking it.
Thanks, Jay. Just what I was looking for.
Regards,
Paul

The pessimist complains about the wind. The optimist expects it to change. The realist adjusts his sails.

User avatar
PaulB
SilverLounger
Posts: 1610
Joined: 26 Jan 2010, 20:28
Location: Ottawa ON

Re: Script to delete files

Post by PaulB »

Rudi wrote:IMHO, Win Explorer is actually very underrated!
I nominate this statement for your Quote of the Week award! I certainly had not thought of Widows/File Explorer as a possible solution to this problem.
Regards,
Paul

The pessimist complains about the wind. The optimist expects it to change. The realist adjusts his sails.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Script to delete files

Post by Rudi »

Some people will completely refute that "quote" as they despise Win Explorer when compared to Command Line or with other 3rd party apps like File Commander, etc. However, it has a number of little gems and shortcuts that help to manage files on your system.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.