Delete Sub-Directories And Files

I came across a situation where I wanted to delete all the files and sub-directories out of a a directory.  I wanted to leave my main directory and just delete every thing in it.

I tried using the del command but it does not remove sub-directories.  There is the rd command to remove directories, but it only removes directories that are empty.  The other problem with the RD command is that you need to know the name of the directory.  The solution was to script a file, which is fine because I wanted to schedule this job to run weekly.  I found a script on the Internet that did what I wanted but I had to modify it slightly.  Below is the script along with explanations in red.

echo off

REM Edit your folder path
set CAT=C:\path name
(Set the path of the folder you wish to empty out)

dir “%%CAT%%”/s/b/a | sort /r >> %TEMP%\files2del.txt
(This line scans the directory and saves a list of all sub-directories)
for /f “delims=;” %%D in (%TEMP%\files2del.txt) do (del /F /Q /a:h “%%D” & rd “%%D”)
(Deletes all hidden files and removes directory if empty)
for /f “delims=;” %%D in (%TEMP%\files2del.txt) do (del /F /Q “%%D” & rd “%%D”)
(Deletes all files and removes empty directory)
del /q %TEMP%\files2del.txt
(Deletes temporary list of sub-directories)

The only modification I made to the script was adding the line that deletes all the hidden files.  I had to do this because the original script would leave sub-directories with hidden Thumbs files.