Simple way to delete folder contents from command line?

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
 
P

Pegasus \(MVP\)

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
 
M

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

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!
 
J

Jerold Schulman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top