Windows 7 - delete all files+empty folders with with specific extension recursively

05
2014-04
  • Lars Holdgaard

    I have tried to follow the advice here: Need to delete all the files with one extension on a drive in Windows 7

    But if I want to delete all files in a specific folder, with a specific extension, that doesn't work.

    I have a folder with ".ASP" and ".ASPX" files, and I need to delete all the "*.ASP" files but keep the ASPX pages.

    I also need to delete a folder if it is empty, after deleting the file.

    How would I delete all the ASP pages easily, including empty folders? Command prompt is no problem.

    EDIT: This this the trick in PowerShell:

    Get-ChildItem -path . -recurse -include *.asp | 
      Where-Object {-not $_.PSIsContainer} |
      Remove-Item -Verbose
    
  • Answers
  • Matthew Williams

    To remove all files with a specific extension can be done with something like:

    cd C:\ path\to\directory\extension
    del /s *.extension

    For removing empty folders I would suggest looking at a question from stackoverflow:

    http://stackoverflow.com/questions/7831286/how-to-delete-empty-folders-using-windows-command-prompt

    EDIT: Obviously I suggest caution running this sort of command on an entire drive. There might be something you need. So do be careful.

  • JdeBP

    Here's the way to do it with JP Software's TCC/LE:

    del /s /x *.asp
    

    The /s option causes recursion into subdirectories, and the /x option causes emptied directories to be removed.

    TCC will not match short filenames with the *.asp wildcard. That sort of wildcard matching is a non-default feature that has to be turned on. The wildcard will just match long filenames, so the problem caused by *.aspx files happening to have *.asp short filenames will not occur.

    Further reading

    • JP Software. DEL. Take Command / TCC Help.
    • JP Software. LFN Searches. Take Command / TCC Help.

  • Related Question

    osx - Delete all files in a folder without deleting directories? (OS X)
  • Mk12

    Is there a simple terminal command maybe to delete all actual data, all files but leave all the directories there? Including packages (.app) as directories?

    -- You don't need to read this: The reason why is on my iPod Touch, whenever I ssh to /private/var/mobile/Applications to get an icon or something to change for a theme, I have to look through every folder to find the application, since they're all in their unique identifier folders (e.g. 2C053638-26FE-42DD-A235-30FCBA59E623), its impossible to find it. So I copied the Applications folder to my desktop, so I could spotlight search for the application name in it and then the folder that its in would be the unique id folder on the iPod, so having it sorted the same I could easily find it.


  • Related Answers
  • David Spillett

    Are you wanting to delete just the files from the current directory, or files from sub-directories too? For the latter this would work under most unix-a-like environments

    find . -type f -print0 | xargs -0 rm -f
    

    or if you know there are no files or directories with spaces in their names you can simplify a little with

    find . -type f | xargs rm -f
    

    I'm not an Apple user so I know little of the .app directories of which you speak, but you should be able to avoid touching them by adding grep between find and xargs like

    find . -type f | grep -v \.app | xargs rm -f
    

    Replace rm -f with ls or ls -l in all the above to get a list of what would be deleted instead of actually performing the delete.

  • Hai Vu

    Find can delete as well:

    $ find . -type f -exec rm {} \;
    

    BE CAREFUL: this command means business--it delete all files starting from the current directory without asking.

  • shellking

    open your terminal application

    (1) cd {path of the iTouch content}

      for example:  cd /Users/Mike/Desktop/myTouch
    

    (2) then recurse through the directories and remove content.

      find {directory} -type f | tee output
    
      for i in `grep -v .app output`
      do
       echo ${i}; rm ${i}
      done
    

    for example,

    find /Users/Mike/Documents/myTouch/ -type f | tee output
    
    for file in `grep -v .app output`
    do
     echo ${file}
     rm ${file}
    done