Discussion:
Deleting empty subdirectories...
(too old to reply)
Laurent Jumet
2014-06-28 08:45:03 UTC
Permalink
Hello !

What's the command to delete empty subdirectories (nested), and only those?
(not the files, only empty subdirectories)
--
Laurent Jumet - Point de Chat, Liège, BELGIUM
KeyID: 0xCFAF704C
[Restore address to laurent.jumet for e-mail reply.]
Stan Brown
2014-06-28 15:28:19 UTC
Permalink
Post by Laurent Jumet
Hello !
What's the command to delete empty subdirectories (nested), and only those?
(not the files, only empty subdirectories)
There isn't one, as far as I can remember.

RD deletes one directory if it's empty, but I don't know of a
recursive variant since it would have to start at the bottom and not
at the top of the structure.
--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
Kenny McCormack
2014-06-28 15:33:29 UTC
Permalink
Post by Stan Brown
Post by Laurent Jumet
Hello !
What's the command to delete empty subdirectories (nested), and only those?
(not the files, only empty subdirectories)
There isn't one, as far as I can remember.
RD deletes one directory if it's empty, but I don't know of a
recursive variant since it would have to start at the bottom and not
at the top of the structure.
dir /ad/s/b > foo.bat
{ edit foo.bat in your favorite editor; insert "rd " at the beginning of each line }

Run foo.bat and ignore any/all error messages.
--
Here's a simple test for Fox viewers:

1) Sit back, close your eyes, and think (Yes, I know that's hard for you).
2) Think about and imagine all of your ridiculous fantasies about Barack Obama.
3) Now, imagine that he is white. Cogitate on how absurd your fantasies
seem now.

See? That wasn't hard, was it?
Laurent Jumet
2014-06-28 16:48:48 UTC
Permalink
Hello Kenny !
Post by Kenny McCormack
Post by Stan Brown
Post by Laurent Jumet
What's the command to delete empty subdirectories (nested), and only
those? (not the files, only empty subdirectories)
There isn't one, as far as I can remember.
RD deletes one directory if it's empty, but I don't know of a
recursive variant since it would have to start at the bottom and not
at the top of the structure.
dir /ad/s/b > foo.bat
{ edit foo.bat in your favorite editor; insert "rd " at the beginning of each line }
Starting from both suggestions, I found out:

dir /ad/s/b | FOR %i in (@CON) RD %i
--
Laurent Jumet - Point de Chat, Liège, BELGIUM
KeyID: 0xCFAF704C
[Restore address to laurent.jumet for e-mail reply.]
Stan Brown
2014-06-28 20:57:13 UTC
Permalink
Post by Kenny McCormack
Post by Stan Brown
RD deletes one directory if it's empty, but I don't know of a
recursive variant since it would have to start at the bottom and not
at the top of the structure.
dir /ad/s/b > foo.bat
{ edit foo.bat in your favorite editor; insert "rd " at the beginning of each line }
Run foo.bat and ignore any/all error messages.
It's a good idea to try a "solution" before you post it. If you had
done that -- as I did -- you'd have found that what I said was
correct.

Example: Create empty directories a, a\b, and a\b\c. Apply your
"solution". a is not deleted because it is not empty (it has a
subdirecory). b is not deleted because ditto.
--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
Kenny McCormack
2014-06-28 22:20:33 UTC
Permalink
In article <***@news.individual.net>,
Stan Brown <***@fastmail.fm> wrote:
...
Post by Stan Brown
Post by Kenny McCormack
Post by Stan Brown
RD deletes one directory if it's empty, but I don't know of a
recursive variant since it would have to start at the bottom and not
at the top of the structure.
True. Good point.
Post by Stan Brown
Post by Kenny McCormack
dir /ad/s/b > foo.bat
{ edit foo.bat in your favorite editor; insert "rd " at the beginning of each line }
Run foo.bat and ignore any/all error messages.
It's a good idea to try a "solution" before you post it. If you had
done that -- as I did -- you'd have found that what I said was
correct.
So, OK. You add a step that sorts the file accordingly, to get bottom-up
order. I am not going to go into it right now, but it should be pretty
easy to do this in AWK or similar.

Or, ...

You just run the batch file over and over until nothing is deleted. (heh heh)

But, yes, your overall point that there isn't a compact (i.e., one-command)
and slick (no weirdness such as I am proposing) way to do this is valid.

I mean, if I had to do it (and I'm assuming that if I had to do it, it
would just be a one-off, not part of a repeating process - and I'm assuming
same of the OP), I'm sure I could easily get it done, but, no, there
doesn't seem to be a easy straightforward single command to do it.

Or, ...
--
Rich people pay Fox people to convince middle class people to blame poor people.

(John Fugelsang)
CRNG
2014-06-29 00:28:54 UTC
Permalink
Post by Kenny McCormack
I mean, if I had to do it (and I'm assuming that if I had to do it, it
would just be a one-off, not part of a repeating process - and I'm assuming
same of the OP),
Bad assumptions.
--
Web based forums are like subscribing to 10 different newspapers
and having to visit 10 different news stands to pickup each one.
Email list-server groups and USENET are like having all of those
newspapers delivered to your door every morning.
Kenny McCormack
2014-06-29 01:20:11 UTC
Permalink
Post by CRNG
Post by Kenny McCormack
I mean, if I had to do it (and I'm assuming that if I had to do it, it
would just be a one-off, not part of a repeating process - and I'm assuming
same of the OP),
Bad assumptions.
How would you know?
--
b w w g y g r b y w
CRNG
2014-06-29 09:23:47 UTC
Permalink
Post by Kenny McCormack
Post by CRNG
Post by Kenny McCormack
I mean, if I had to do it (and I'm assuming that if I had to do it, it
would just be a one-off, not part of a repeating process - and I'm assuming
same of the OP),
Bad assumptions.
How would you know?
You post untested crap that doesn't work, therefore bad assumptions or
you are a moron.
--
Web based forums are like subscribing to 10 different newspapers
and having to visit 10 different news stands to pickup each one.
Email list-server groups and USENET are like having all of those
newspapers delivered to your door every morning.
Axel Berger
2014-06-29 14:37:49 UTC
Permalink
Post by Stan Brown
If you had
done that -- as I did -- you'd have found that what I said was
correct.
Naturally. There are two easy solutions:
a) Just run the batch several times or call it, say, ten times in a
loop.
b) While assembling it in an editor
- number lines
- sort in reverse order
- delete line numbers
Then it should work even when run once.

Axel
Axel Berger
2014-06-29 14:38:04 UTC
Permalink
Post by Stan Brown
If you had
done that -- as I did -- you'd have found that what I said was
correct.
Naturally. There are two easy solutions:
a) Just run the batch several times or call it, say, ten times in a
loop.
b) While assembling it in an editor
- number lines
- sort in reverse order
- delete line numbers
Then it should work even when run once.

Axel
Klaus Meinhard
2014-06-29 10:34:42 UTC
Permalink
Hallo Laurent Jumet,
Post by Laurent Jumet
What's the command to delete empty subdirectories (nested), and only
those? (not the files, only empty subdirectories)
This works in e.g. newer versions of TCC/LE or TCC. I doubt 4DOS's
@files already had the /h parameter.

You can find emty subdirs with

for /r %d in ( nul ) if %@files[/h "%d\.."] == 0 echo %@path[%d]

and delete those. Since a subdir with another nested subdir isn't empty,
you'd have to do this repeatedly until all empty dirs are deleted.

Of course you could write a really massive batch file to do the job (I
remember I have written XIN and XOUT batches to walk systematically down
a directory tree and up again (available in my 4XBTM batch collection).

That said, I recommand some caution removing empty subdirs. The mere
existence of a subdir may be meaningful for its creator (usually a
programm). Its easy to delete more than one wished for.
--
Herzliche Grüße,

* Klaus Meinhard *
Laurent Jumet
2014-06-29 14:12:58 UTC
Permalink
Hello Klaus !
Post by Klaus Meinhard
Post by Laurent Jumet
What's the command to delete empty subdirectories (nested), and only
those? (not the files, only empty subdirectories)
This works in e.g. newer versions of TCC/LE or TCC. I doubt 4DOS's
@files already had the /h parameter.
That said, I recommand some caution removing empty subdirs. The mere
existence of a subdir may be meaningful for its creator (usually a
programm). Its easy to delete more than one wished for.
...yes.
It's not recommended to delete empty subdirs, as they might be waiting for
something.
In my case, it's a cache that does files maintenance, but not dirs maintenance; I
get hundreds of empty dirs.
--
Laurent Jumet - Point de Chat, Liège, BELGIUM
KeyID: 0xCFAF704C
[Restore address to laurent.jumet for e-mail reply.]
Stan Brown
2014-06-29 21:04:25 UTC
Permalink
You have posted this before. It's a pity you still haven't tried it.
If you had, with a simple test case like subfolder c under b under a,
you would find that it removes c but not b.
--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
hopflop
2014-06-29 15:52:22 UTC
Permalink
Post by Laurent Jumet
Hello !
What's the command to delete empty subdirectories (nested), and only those?
(not the files, only empty subdirectories)
del /a:d /s /x *
--
hopflop
Kenny McCormack
2014-06-29 16:02:42 UTC
Permalink
Post by hopflop
Post by Laurent Jumet
Hello !
What's the command to delete empty subdirectories (nested), and only those?
(not the files, only empty subdirectories)
del /a:d /s /x *
I think that has the same problem as all the other solutions (except maybe
Klaus's; I haven't really looked at that one closely) do - that it won't
remove directories that contain other directories. As Stan observes, it is
necessary to do it "bottom up" if this is what you really want.

I like Axel's clever idea of just reversing the file; that should do it.

P.S. As I've been arguing all along, if this is a one-off, then it is
best/easiest to do it in an editor. If it a part of an on-going process,
then, of course, you will want to script/automate it.
--
"The smart way to keep people passive and obedient is to strictly limit the
spectrum of acceptable opinion, but allow very lively debate within that
spectrum...."

- Noam Chomsky, The Common Good -
Stan Brown
2014-06-29 21:07:19 UTC
Permalink
Post by hopflop
[quoted text muted]
What's the command to delete empty subdirectories (nested), and only those?
(not the files, only empty subdirectories)
del /a:d /s /x *
Nicely done -- I would not have thought of using DEL to remove a
directory, but I tried it and it works. Thanks for posting.
--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
Loading...