1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Delete Contents of folder using command line

Discussion in 'Windows XP' started by FatherCrowe, Jan 20, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. FatherCrowe

    FatherCrowe Thread Starter

    Joined:
    Mar 31, 2005
    Messages:
    85
    Hi All,

    Trying to set up some scripts to automate a few processes.

    I need to be able to delete the contents of a folder/s including subfolders BUT not to delete the top folder itself.

    Looking to find the find the command line code for this.

    If anyone has any ideas that would awesom!

    Thanks in advance.

    FC
     
  2. DKTaber

    DKTaber

    Joined:
    Oct 26, 2001
    Messages:
    2,871
    Open the Run box (Start, R); type "cmd" in the Run box and click OK. In the DOS window that opens, use "cd\" to move to the root directory (command line will say only "C:\>". Now type "cd [name of folder from which you want to delete files]", substituting the actual name of the folder -- e.g., "cd\JunkyPlace" -- and hit enter. Then type "del *.*". You will be asked if you're sure. Hit the 'y' key. All files will be deleted.
     
  3. FatherCrowe

    FatherCrowe Thread Starter

    Joined:
    Mar 31, 2005
    Messages:
    85
    DKTaber, thanks for the reply. Unfortunately the *.* command doesn't work.

    Can't remember the error but when I googles the issue there are a lot of people reporting the same thing.

    When I'm back in front of the PC I'll see what the error is.

    Thanks.
     
  4. DKTaber

    DKTaber

    Joined:
    Oct 26, 2001
    Messages:
    2,871
    That's probably because some file in that folder is open. The delete command will not delete any file that's open. Try doing the same thing in Safe Mode.
     
  5. surferdude2

    surferdude2

    Joined:
    Jul 6, 2010
    Messages:
    619
    Here's what you'll find when using the command line with XP's native tools:

    The RD (RemoveDirectory) command can ONLY remove empty directories.
    The Del (Delete) command can ONLY remove files.

    Therefore, you can't do what you are wanting with a single command. Not only that, the command for removing directories (RD) that is supplied by XP will not remove a directory that contains files of any kind, even hidden files. That makes it necessary to clean all files from all subdirectories before you will be allowed to remove any parent directory. That gets to be labor intense in some situations.

    If you really need to ignore the easy GUI of XP and do this deed using command line syntax, I suggest that you get a copy of the Deltree command. You can get it from an older Windows system or download it from the internet. Google it for an easy find. You would have to place it on your PATH, such as in the Windows\system32 folder.

    The Deltree command has greater power than the RD command and will dump directories regardless of their content. As with any powerful command, be sure you know what you're doing!!

    By using Deltree you can issue one simple command and it will clean all directories below any point that you specify.

    Then, assuming you want to delete the files left in the parent folder, you will need to issue the Del command to delete the remaining files located there. The wildcard operator (*.*) will work on those when using the Del command.

    As always, when plowing new ground, good backups are highly recommended.
     
  6. DKTaber

    DKTaber

    Joined:
    Oct 26, 2001
    Messages:
    2,871
    Strange. . . I have XP (SP3) and frequently delete folders containing files -- sometimes LOTS of files. There are certainly times when the OS refuses to delete a folder, but it's because a file within that folder is open/being used by something or it contains a file(s) whose permissions don't allow the user deletion privileges. What I typically do when I encounter "deletion blocks" like that is reboot to Safe Mode with Command Prompt so nothing is using the file(s), then use standard DOS commands to maneuver to the folder and "del *.*". Don't remember ever having that process fail.
     
  7. surferdude2

    surferdude2

    Joined:
    Jul 6, 2010
    Messages:
    619
    Perhaps you are not trying that in command mode. Windows GUI can handle folders with content, even though it balks at times, whereas the RD command always stumbles.

    Yes, now you are using the Del command and it works on FILES ONLY and also accepts the wild card operator. Then you still must use the RD command to remove the remaining directory.

    Here is a direct screen copy of what I get when trying to delete a directory named test that contains one single file named test.doc within:

    Volume in drive Z is z640gig D4P1
    Volume Serial Number is D404-6B8E
    Directory of Z:\test
    01/21/2011 08:30 AM <DIR> .
    01/21/2011 08:30 AM <DIR> ..
    01/21/2011 08:28 AM 0 Test.doc
    1 File(s) 0 bytes
    2 Dir(s) 211,870,769,152 bytes free
    Z:\>rd test
    The directory is not empty.

    If your system works differently, I'd be surprised. It would tend to prove that there is an exception to every rule. ;)
     
  8. throoper

    throoper Trusted Advisor

    Joined:
    Jan 20, 2007
    Messages:
    5,176
    Try the following batch file:
    Code:
    cls
    @ECHO OFF
    RD /S /Q [path to directory]
    MD [path to directory]
    The /S switch will remove all files and sub-directories along with the parent directory.
    The /Q switch will suppress the confirm deletion prompt.
    The MD will recreate the parent folder.
     
  9. surferdude2

    surferdude2

    Joined:
    Jul 6, 2010
    Messages:
    619
    That works and proves that MS has contradictions in their instructions. Of course that's not news. ;)

    Here's how MS describes their Rmdir (RD) command:

    Go figure. :D :D

    Thanks for the insight. Looks like I won't need my old trusty Deltree command much. I have it in so many batches though, I think I'd better keep it. ;)
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/975829

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice