remove sub directories and files

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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"
)
 
Another alternative would be to remove the directory and then recreate it

rd /s /q "directorypath"
md "directorypath"
 
Back
Top