How to delete folder contents RRS feed

  • Question

  • Hi,

    In windows 2003 server. I want to delete certain files in specific folders spread in multiple locations. For example, i want to delete all excel files in the folder FOLDER1. Folder1 is located many places in the hard disks.

    C:\documents and settings\user1\folder1\sgdfd.xls
    C:\documents and settings\user2\folder1\sfsd.xls
    C:\documents and settings\user3\folder1\sdfss.xls


    C:\documents and settings\user200\folder1\dss.xls

    I dont want to delete the whole folder of FOLDER1. How to delete only the excel files in the FOLDER1 directories recursively?

    Thanks a lot

    Saturday, June 30, 2012 12:52 PM

All replies

  • Hello Bash, 

    I suggest you check this script link to accomplish it.

    Delete files with specific extension:http://gallery.technet.microsoft.com/scriptcenter/b2cb61dc-854a-4fdf-80da-363c1203609c

    Note:Run this script in your test lab before applying to live environment.

    Regards, Ravikumar P

    Saturday, June 30, 2012 3:29 PM
  • as mentioned for the other suggeston already, test these commands before you try it in production...

    the del command line command has the /s switch to go through subdirectories

    so a command like

    del c:\temp\deltest\*.xls /s

    would delete all xls files in c:\temp\deltest and its subdirectories
    • Proposed as answer by screameryBanned Wednesday, July 4, 2012 6:52 AM
    • Marked as answer by Jeff Ren Friday, July 6, 2012 1:51 AM
    • Unmarked as answer by Bashboosh Friday, July 6, 2012 7:42 PM
    Saturday, June 30, 2012 6:51 PM
  • You will have to also take into account user permissions; therefore, you will need to do this as the domain or local admin.

    Create a simple batch file similar to below.

    set docssets="\documents and settings"
    cd %docssets%
    del /S /Q *.xls

    You have to be careful with this as this will delete all Excel files--even those that the user may wanted to keep.  If you have some sort of naming convention, you can limit to just those and leave others alone.

    A better solution would be to create a batch file that incorporates a FOR loop that will descend into the FOLDER1 directly and delete the Excel files from there.

    Open a command prompt and type FOR /? for syntax and usage.

    Saturday, June 30, 2012 7:24 PM
  • Hi Darien,

    Thanks for your reply. I am not sure it wil work because, in my example, userprofile directory will be varying. But the FOLDER1 directory is unchanged. I dont care wherever the files sit, i just wanted to delete all files under the directory of FOLDER1. this is my criteria.

    Thanks again and waiting for any update.

    Thursday, July 5, 2012 10:14 AM
  • It is better to discuss scripting queries here.



    • Marked as answer by Jeff Ren Friday, July 6, 2012 1:51 AM
    • Unmarked as answer by Bashboosh Friday, July 6, 2012 7:41 PM
    Thursday, July 5, 2012 10:29 AM
  • Hi,

    If it canot be solved from command prompt, no problem to move to scripting forum.


    Friday, July 6, 2012 7:43 PM
  • Hi,

    I think you can use del *.xls /s to delete those files by writing a login scripts.

    For files located in user profile, you may use environment variable such as %userprofile% to point to the profile of specific user.  

    Saturday, July 7, 2012 1:09 AM