Recursively Delete Files in Folders and Sub Folders Using Command Prompt
By on July 29th, 2008

One of our readers asked us a real interesting question, which we really loved to solve. The question was tricky but the solution was real simple.

The question asked to us was;

Do you know if there’s a way to delete files of a particular filetype from a set of folders quickly in Windows?

For example, I’ve got a folder by the name “ABC” and I’ve got some pictures and music files there, and I need a tool that automatically deletes all those pictures (i.e., files of a particular format like JPEG) quickly from the ABC folder and its sub-folders.

The task of deleting files recursively from folders and sub-folders does not require any tools and can be easily done using the Windows command prompt. For this post we will assume that the directory, you want to delete the files from is D:ABCand it contains several sub-folders from which you want to delete the files.

Note: Before using the command, we want to warn you that the files will be completely deleted bypassing the recycle bin. If you are looking for a safer way to delete files, we can point you to a earlier post where we introduced the recycle command, using which you can safely delete files to the recycle bin using the command prompt, you can also use wildcards to recycle the files.

How to Delete files recursively in folders and sub-folders using the command prompt?

To delete the files, first open a command prompt window and navigate to the folder you want to delete the files from. To do that use the Windows + R hotkey to open the run window, type in cmdwithout the quotes and hit enter.

Once the command prompt has opened navigate to the folder you want to recursively delete the files from. Once you are in the folder, issue the following command:

del /S test.txt

This command will delete all the files named test.txt inside the current folders, as well as from all the sub-folders present in that folder. The /Sdirective tells the del command to delete all the files recursively from within the folder and sub-folders. See screenshot below.

delete-files-recursively

You can also use wildcards to delete files of only a particular type, the command to delete files recursively based on wildcards will look like:

del /S /P *.jpg

Note: The /P tells the command to prompt the user before deleting the file, we recommend using this with wildcards.

delete-files-recursively-with-wildcards

If you want to delete files from only the current directory change the command as below:

del *.jpg

That’s it simple and easy without having to use any additional tools. If you have your own questions don’t forget to ask us. we will always try our best to find a solution for you.

Tags: , , ,
Author: Keith Dsouza Google Profile for Keith Dsouza
I am the editor-in-chief and owner of Techie Buzz. I love coding and have contributed to several open source projects in the past. You can know more about me and my projects by visiting my Personal Website. I am also a social networking enthusiast and can be found active on twitter, you can follow Keith on twitter @keithdsouza. You can click on my name to visit my Google+ profile.

Keith Dsouza has written and can be contacted at keith@techie-buzz.com.

Leave a Reply

Name (required)

Website (optional)

 
    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'advanced_comment' not found or invalid function name in /home/keith/techie-buzz.com/htdocs/wp-includes/comment-template.php on line 1694
 
Copyright 2006-2012 Techie Buzz. All Rights Reserved. Our content may not be reproduced on other websites. Content Delivery by MaxCDN