none
Command line recursive directory delete

    Question

  • Hello,

    From the command prompt I need to be able to recursively delete all contents of a directory but not the directory itself.  For example, if my command window is opened in a particular directory and that directory contains subdirectories named ABC, DEF, and GHI, let's say I need to be able to delete all files and subdirectories in subdirectory ABC, but not subdirectory ABC itself.  I've tried rd /Q /S ABC and rmdir /Q /S ABC but those also delete ABC itself.  I've also tried del /Q /S ABC but that only deletes the files and not the directories.

    Thanks,
    Ray

     

    Tuesday, June 14, 2011 7:20 PM

All replies

  • cd ABC

    use for to delete each file/folder (type for/? or visit a scripting community such as the alt.msdos.batch.nt newsgroup or the Official Scripting Guys forum on Technet if you need help on that)

    cd ..



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, June 14, 2011 8:17 PM
  • Sheng,

    Thanks for the suggestion.  I thought about doing a CD first like you suggest, but I consider that pretty risky.  If for some reason the CD should fail (and such strange things do happen), then if I did a recursive delete of *.* I would end up wiping out everything in the original directory.  I was hoping there was a convenient way it could be done that involved explicity naming the subdirectory.  It's actually not very difficult to use C to write my own command utility that does this but I was hoping something standard was already out there.

    Thanks,
    Ray

     

    Tuesday, June 14, 2011 11:00 PM
  • You can probably check if the current directory is changed before wiping out

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Sunday, June 19, 2011 6:34 AM
  •  I've tried rd /Q /S ABC and rmdir /Q /S ABC but those also delete ABC itself. 


    rmdir /s ABC && mkdir ABC 

     

    Sunday, June 19, 2011 8:57 AM
  •  I've tried rd /Q /S ABC and rmdir /Q /S ABC but those also delete ABC itself. 


    rmdir /s ABC && mkdir ABC 

     


    On my Win7 installation what you suggest above still deletes ABC also, which is not what I want.  Thanks anyway.
    Monday, June 20, 2011 1:38 AM
  • Then... install some unix shell (or SFU) and you'll have rm -rf ABC/*

     -pa


    Monday, June 20, 2011 7:36 AM
  • Then... install some unix shell (or SFU) and you'll have rm -rf ABC/*

     -pa



    I'm afraid that sounds a little drastic just to get one simple command.  I think writing my own command is probably the most reasonable approach for general and quick portable use, although I believe many "missing" commands have probably already been written by more than one frustrated programmer.
    Monday, June 20, 2011 8:25 AM