Batch Scripting - Delete all Directories within a Directory

Discussion in 'Web Design & Coding' started by madmatt, Aug 24, 2006.

  1. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    DEL only deletes files
    RD not only deletes the contents of a directory but the directory itself

    I need a command that will delete only the contents (including other directories) of a directory.

    I don't know all the directory names within this directory so it needs to do the work for me.

    Help ASAP is appreciated. Thank you.
     
  2. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    del should delete directories as welll :s

    del *.*/s would delete all files and subdirectories
     
  3. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    It doesn't. It only deletes the contents of those directories and not the directories.
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    so do del *.* /s and then rd *?
     
  5. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    RD will delete the base directory. I only want to delete sub directories.

    e.g.

    C:\Work
    C:\Work\Temp1\
    C:\Work\Temp2\
    C:\Work\filename.txt

    I want to delete everything EXCEPT C:\Work
     
  6. lancer

    lancer There is no answer! Political User Folding Team

    Messages:
    3,093
    Location:
    FL, USA
    so wouldn't deltree c:\work\Temp1 work? then just do the same for the others. i don't think there is a way to specify deleting all the subdirectories within work all together.

    or you could do this

    DELTREE /Y c:\work\temp1 c:\work\temp2 del c:\work\filename.txt

    i think thats it, maybe wrong someplace.
     
    Last edited: Aug 24, 2006
  7. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Install unix services for Windows and use bash to write a shell scripts which does this for you:

    rm -rf *

    Done :D
     
  8. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    Deltree is obsolete because the RMDIR command now includes the /S option which removes folders that aren't empty. Combine it with the /Q and you won't even be prompted to confirm.

    I could probably write something using the FOR command, I'll have to look into it later as my lunch is almost over :D

    -EDIT-

    Ok something quick I slapped together....

    Code:
    RD C:\TEST\TEST\NEW /s /Q
    
    Put that in a batch file, it will go down to the C:\TEST\TEST\ directory and delete the "NEW" folder.

    This could be tweaked, so that it skips down to your potential directories, such as c:\work\ and then afoheaofheaiohfe

    OK, edit again :D

    I just tested it and try this :D

    Code:
    @ECHO OFF
     
    RD C:\WORK\ /s /Q
     
    
    That will delete all files and directories including the work folder

    I'll play later, but you are close.

    I suppose you could do something like this...

    Code:
    RD C:\Work\Folder1 /S /Q
    RD C:\TEST\Folder2 /S /Q
    
    Just let the commands run, using the /S and /Q switches appropriately
     
    Last edited: Aug 24, 2006
  9. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Yes, DELTREE is obsolete.

    RM/RMDIR would work but it deletes the base directory which is bad.
     
  10. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    ^^^Scared the crap outta me when I was testing, I had Windows Explorer in one of those directories and then all the sudden it changed to c:\windows :eek:

    I was like - crap!

    OK, this works for any and all subfolders, changing the path and folder names appropriately -

    Code:
    @ECHO OFF
    RD C:\TEST\test1 /s /Q
    RD C:\TEST\test2 /s /Q
    RD C:\TEST\test3 /s /Q
    
    As long as there are only folders in the root you want to keep, and not files, that should do it?

    If there are files, I think you could just add a DEL *.* on there from say -

    DEL C:\TEST *.*

    But if you force to directories, you can mix and match commands.

    Use a DEL in the root folder for any files that may be there,
     
  11. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    I don't know the names of the directories within "C:\TEST\".
     
  12. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    Ah, I can see how that would put a wrinkle in things.

    Let me keep fiddling.....
     
  13. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    give me a couple minutes.. i'll whip up something for you

    edit: here ya go
    Code:
    @echo off
    dir /b /ad %1 > dirlist.txt
    for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
    del %1\*.*
    del dirlist.txt
    
    Save it to a batch file.. run it from the command line and pass the base directory to it as a parameter.

    Example:
    Let's say the batch file is called.. "delsub.bat" and it's saved to the root of the C: drive.. and you have a directory c:\work that you want to delete all the sub directories from.. you would do as follows:

    c:\> delsub c:\work

    IMPORTANT: you shouldn't add a trailing "\" to the directory name you pass.

    edit2:
    If you want to know how it works..
    @echo off
    Hopefully you know what this does..

    dir /b /ad %1 > dirlist.txt
    Creates a file called dirlist.txt in the current working directory that contains a list of all the the subdirectories of the base directory. The "/b" does a bare listing (only the names.. none of the dates, etc). The "/ad" switch just lists the directories. The "> dirlist.txt" portion redirectes the output of the command to a file called dirlist.txt

    for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
    Loops through the dirlist.txt file reading the directory names and runs the command "rd /s /q <directory name> for each subdirectory listed in the file. I won't go into all the switches available with the for command.. running "for /?" from the command prompt will display several pages of info.

    del %1\*.*
    Deletes all the files from the base directory

    del dirlist.txt

    Just does a little cleanup and deletes the dirlist.txt file it created. You can comment/delete this line out if you to retain the list of directories that were deleted.

    edit3: made some changes because I noticed in an earlier post that you did want to delete the files out of the base directory as well.
     
    Last edited: Aug 24, 2006
    madmatt likes this.
  14. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    I knew the for command was the way to go, I'm such a newb :(
     
  15. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Thank you fitz... cookies on the way!
     
  16. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    Woo! Cookies!
     
  17. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    Instead of looping through all of the subdirectories, how about this:

    RD %1\ /s /q
    MD %1\

    Just remove the parent directory and it's contents, then add just the parent directory back. But I don't know if there is a permission issue when the parent directory is a system directory or not.
     
  18. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    That would probably work..

    but .. what if you wanted to delete all the directories on a network share.. and the network share is your root? you couldn't do a:

    rd /s /q \\server1\sharename

    but using the batch file with a loop would work.. okay.. i'm making excuses for why my way is better :)

    edit: oh! oh! and what if you wanted to keep the files in the base directory and just delete all the sub directories.. then my way is better because you could remove the line to delete the files!
     
  19. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    Your way is better, I have no doubt - I was just playing with it and found that way to work too.

    Your way is better because my way can't be used in the event that the parent folder is a system folder - you would receive access denied errors :(

    But Hey, I'm just an apprentice compared to you - I'm learning :)
     
  20. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    lol.. i'm always learning new things :)

    Your way is rather simple and would work for most situations (the KISS method is always a good thing!) and it does show some imagination/troubleshooting ability that I often find lacking in many people who work in IS/IT and claim to be IS/IT professionals.

    I find it is not necessary how much you know, but the ability to learn and approach problems from different ways that make one person a better tech than another.

    Ok.. off my soapbox now..