Solved: Delete Contents of folder using command line

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 
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.
 

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.
 
Joined
Oct 26, 2001
Messages
2,871
DKTaber, thanks for the reply. Unfortunately the *.* command doesn't work.
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.
 
Joined
Jul 6, 2010
Messages
619
I need to be able to delete the contents of a folder/s including subfolders BUT not to delete the top folder itself.
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.
 
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.
 
Joined
Jul 6, 2010
Messages
619
Strange. . . I have XP (SP3) and frequently delete folders containing files -- sometimes LOTS of files.
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.

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.
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. ;)
 

throoper

Trusted Advisor (deceased)
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.
 
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:

Rmdir (rd)

Removes (that is, deletes) a directory.
Syntax

rmdir [Drive:]Path [/s] [/q]
rd [Drive:]Path [/s] [/q]
Parameters

[Drive:]Path Specifies the location and name of the directory that you want to delete. /s Removes the specified directory and all subdirectories including any files. Use /s to remove a tree. /q Runs rmdir in quiet mode. Deletes directories without confirmation. /? Displays help at the command prompt. Remarks

  • Using rmdir at the Recovery Console The rmdir command, with different parameters, is available from the Recovery Console.
  • Cannot delete directory with hidden or system files You cannot delete a directory that contains files, including hidden or system files. If you attempt to do so, the following message appears:
    The directory not empty
    Use the dir command to list hidden and system files, and the attrib command to remove hidden and system attributes from files. For more information, see Related Topics.
  • Using the backslash character with the path parameter If you insert a backslash (\) before the first directory name in path, the directory is treated as a subdirectory of the root directory, regardless of your current directory. If you do not insert a backslash before the first directory name in path, the directory is treated as a subdirectory of the current directory.
  • Deleting the current directory You cannot use rmdir to delete the current directory. You must first change to a different directory (not a subdirectory of the current directory) and then use rmdir with a path. If you attempt to delete the current directory, the following message appears:
    The process cannot access the file because it is being used by another process.
Examples

To delete a directory named \User\Smith, first ensure that the directory is empty. To do this, type:
dir \user\smith /a
Only the "." and ".." symbols should display.
Then, from any directory except \User\Smith, type:
rmdir \user\smith
To delete the directory \User and all of the subdirectories and files, type:
rmdir /s \user
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. ;)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top