remove sub directories and files

G

Guest

Hi all. Currently my company require me to write a batch file to delete files
and sub directories of a share folder. I tried both del and rd command. It
seems that del command only delete files but not sub folders while rd command
remove everything inlcuding the folder that store subfolders and files. Is
there a command or 3rd party tool i can use to delete files and sub folders
of a folder while maintaining the main folder? Thks in advance.
 
P

Pegasus \(MVP\)

inenewbl said:
Hi all. Currently my company require me to write a batch file to delete files
and sub directories of a share folder. I tried both del and rd command. It
seems that del command only delete files but not sub folders while rd command
remove everything inlcuding the folder that store subfolders and files. Is
there a command or 3rd party tool i can use to delete files and sub folders
of a folder while maintaining the main folder? Thks in advance.

There is no need to use third-party tools. Try this:

@echo off
cd /d "d:\Some Folder"
echo Deleting files in %cd%
del /q *.*
for /d %%a in (*.*) do (
echo Deleting %cd%\%%a
rd /s /q "%%a"
)
 
J

Jon

Another alternative would be to remove the directory and then recreate it

rd /s /q "directorypath"
md "directorypath"
 

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