Here is a simple approach to this problem:
dir <Target Dirctory> /b /ad /s | Sort /r > <output file>
EX: dir c:\Windows /b /as /s | sort /r >DirList.TXT
and append RD for each line on of the output file
The concept here is get the directory list and sort it reverse order. Why in reverse order since the directory display the top directory then it goes to it contents. By reversing the order you will remove the inner folder first, then the outer folder.
Since RD (Remove Directory) removes empty folder only, therefore it will just remove empty folders only.
Here is a PERL function I made:
sub EraseDir()
{
if (!-d $_[0])
{
return 0; #Function Failed
}
else
{
#Get the directory list and sort it reverse order.
system ("dir $_[0] /b /ad /s | Sort /r > _junk.file");
open LIST ,"_junk.file" || return 0; #Function Failed
open FH,">_junk.Bat";
foreach my $line (<LIST>)
{
chop $line;
print FH "RD \"$line\"\n";
}
close (FH);
close (LIST);
unlink "_junk.file";
system ("_junk.BAT");
unlink "_junk.Bat";
return 1; #Successful
}
}