Hot Deals: Sweet Valentine Gifts at Sweet Prices | Amazon Kindle DX | Nokia N900 | Nokia E72 | Browse Full Store

Recursively Delete Files in Folders and Sub Folders Using Command Prompt

by Keith Dsouza | Translate | Print
Tuesday, 29th Jul 2008 | Share






Share Recursively Delete Files in Folders and Sub Folders Using Command Prompt on Twitter Share Recursively Delete Files in Folders and Sub Folders Using Command Prompt on Facebook Save Recursively Delete Files in Folders and Sub Folders Using Command Prompt To Delicious Favorites Stumble Recursively Delete Files in Folders and Sub Folders Using Command Prompt Share Recursively Delete Files in Folders and Sub Folders Using Command Prompt on Digg Get Instant Updates as RSS Feeds from Techie Buzz

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:ABC” and 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 “cmd” without 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 “/S” directive 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.

Liked this post? Have a Question? Comment and share your opinions and views with us

Get Regular Updates. Subscribe to Free RSS Feeds or Email Updates. Click to expand subscription options

6 Responses so far | Leave Your Comments

Comments are moderated. Please try and stay on topic. Please do not use abusive language, if you think there is a problem, please email the post author or use the contact us form to tell us about it.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  1. Sandip
    July 29th, 2008 at 11:56 pm #

    Oo even after using Windows for more then 8 years i didn’t know that /S switch with del command, Nice finding Keith.

    Reply to this comment

  2. Nathan
    September 30th, 2008 at 4:04 pm #

    While I have in the past used this command, in recent years I’ve taken to opening the said folder in Windows Explorer, pressing CTRL-F (for find) and typing in the file name with wild cards (ie *.jpg). My search defaults have been set to search hidden and folders and files, too.

    When the results come up, I can sort by file name, folder name, date, etc and select just the ones I want to delete, or press CTRL-A (select all) and hit delete. Added advantage, they go to the recycle bin, which I can empty once I’m sure I didn’t screw up and delete something by mistake!

    Cheers,

    Nathan

    Reply to this comment

  3. Gert-Jan
    February 9th, 2009 at 6:17 am #

    Great stuff, much better than removing thousands of files using Windows Explorer. I used the /S command to delete all individual files that I now have stored in NFileStorage.

    Reply to this comment

  4. Pigeon
    February 19th, 2009 at 12:12 pm #

    Awesome, was looking for something to do this for a while.

    Thanks a lot

    Reply to this comment

  5. Jaymie
    August 11th, 2009 at 10:38 am #

    Is there a way to do this with folders? I’m trying to delete Subversion folders (called “.svn”) from a tree structure, but Windows (XP) Find says it can’t find the folders on disk when I delete them (even though they show up in the search results).

    Reply to this comment

    Glen Reply:

    I found this page looking to remove the .svn folders after copying my repo to another folder and the best advice for deleting these folders was from Nathan above with just using windows find. Del will delete files recursively but not folders.

    Reply to this comment





Better Tag Cloud