Simple way to delete folder contents from command line?

  • Thread starter Thread starter Mark Berry
  • Start date Start date
M

Mark Berry

Hi,

This seems like it should be an easy task, but I don't see an easy solution.
What command can I run from the command line to delete all the files and
subfolders in a folder, without deleting the folder itself?

DEL /S /Q *.* will delete all the files but not the subfolders
RMDIR /S /Q *.* does not work

I know I could go up one level and use RMDIR on the folder itself, but I
want the folder to remain (with its custom permissions).

Is there no solution short of writing a script to loop through the
subfolders?

Background: an IP camera uses recording software to store images in a
folder. It creates its own hierarchy within a specified folder, based on
date and time. Since the image files use a lot of disk space, I want to
automatically delete them after I back them up to tape. However, the
top-level folder must remain so the camera software can continue saving
files.

Thanks for any tips,

Mark Berry
 
Mark Berry said:
Hi,

This seems like it should be an easy task, but I don't see an easy solution.
What command can I run from the command line to delete all the files and
subfolders in a folder, without deleting the folder itself?

DEL /S /Q *.* will delete all the files but not the subfolders
RMDIR /S /Q *.* does not work

I know I could go up one level and use RMDIR on the folder itself, but I
want the folder to remain (with its custom permissions).

Is there no solution short of writing a script to loop through the
subfolders?

Background: an IP camera uses recording software to store images in a
folder. It creates its own hierarchy within a specified folder, based on
date and time. Since the image files use a lot of disk space, I want to
automatically delete them after I back them up to tape. However, the
top-level folder must remain so the camera software can continue saving
files.

Thanks for any tips,

Mark Berry

Try this while inside the parent folder:
del /q *.*
for /d %a in (*.*) do rd /s /q "%a"

or alternatively, while outside the parent folder:
rd /s /q SomeFolder
md SomeFolder
 
Try this while inside the parent folder:
del /q *.*
for /d %a in (*.*) do rd /s /q "%a"

or alternatively, while outside the parent folder:
rd /s /q SomeFolder
md SomeFolder

Outstanding! I had thought about the second approach but it wouldn't
preserve the folder permissions. However, I had never used the FOR syntax,
and it's exactly what I need. Thanks very much!
 
See tip 0617 » How do I remove all files and sub-directories from a folder, without removing the folder?
in the 'Tips & Tricks' at http://www.jsifaq.com

Hi,

This seems like it should be an easy task, but I don't see an easy solution.
What command can I run from the command line to delete all the files and
subfolders in a folder, without deleting the folder itself?

DEL /S /Q *.* will delete all the files but not the subfolders
RMDIR /S /Q *.* does not work

I know I could go up one level and use RMDIR on the folder itself, but I
want the folder to remain (with its custom permissions).

Is there no solution short of writing a script to loop through the
subfolders?

Background: an IP camera uses recording software to store images in a
folder. It creates its own hierarchy within a specified folder, based on
date and time. Since the image files use a lot of disk space, I want to
automatically delete them after I back them up to tape. However, the
top-level folder must remain so the camera software can continue saving
files.

Thanks for any tips,

Mark Berry

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 
Back
Top