In a *NIX system, you can easily type
rmdir * to remove all empty directories in
.. rmdir will complain about non-empty directories and not delete them.
Basically, if you have a tree like this:
/ d0/ file d1/ d0/ d2/ d3/
rmdir * from within
/ will result in
d3/ being deleted only.
d1/ will be left alone with an error echoed about them being non-empty, so as to prevent the contents from being lost in limbo.
/ d0/ file d1/ d0/
I have not been able to find a Windows equivalent to this. The closest thing is
rmdir /s ., which is equivalent to
rm -r ., except it refuses to delete
. because the shell is using it. Not sure if
rm -r . also refuses to delete
.. Haven't tested that.
rmdir will refuse to delete a single directory if non-empty. However, there doesn't seem to be a wildcard selector like the typical
*, which is strange because that token can be used anywhere else.
It's this kind of stuff that frustrates me with the Windows command line. If I have 4,000 directories,
rmdiring every single directory explicitly is obviously not a sane option. There must be a better way.
I've tried building a
for statement according to the syntax in the usage help (
FOR %variable IN (set) DO command [command-parameters]
for %f in * do rmdir %f
and several variations of that (like changing the second
%f%, all-caps'ing the syntax even though I know it's case-insensitive, surrounding
* with parens, et al). None of which worked. All failing with unexplained and unhelpful errors like:
%f was unexpected at this time. * was unexpected at this time.
(NB: I'm attempting this on the interactive prompt, not in a script)
Or in the case of the
>for %f in (*) do rmdir %f >rmdir file The directory name is invalid. >dir Volume in blah blah blah Directory of blah blah blah <DIR> . blah blah <DIR> .. blah blah <DIR> nonemptydir blah blah <DIR> emptydir blah blah 1 file 1 File(s) 1 bytes 4 Dir(s) huge number bytes free
...it doesn't even touch the directories.
I don't understand why it would do that. I followed the syntax as it was described in the help.
Powershell is not an option. I need to be able to do this in a shell that is guaranteed to be installed, i.e. Jeff Zeitlin informed me that Powershell is guaranteed >= Windows 7. However,
cmd.exe is still preferred.