Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting empty subdirectories...

18 views
Skip to first unread message

Laurent Jumet

unread,
Jun 28, 2014, 4:45:03 AM6/28/14
to
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

unread,
Jun 28, 2014, 11:28:19 AM6/28/14
to
On Sat, 28 Jun 2014 10:45:03 +0200, Laurent Jumet wrote:
>
> 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

unread,
Jun 28, 2014, 11:33:29 AM6/28/14
to
In article <MPG.2e18a91f5...@news.individual.net>,
Stan Brown <the_sta...@fastmail.fm> wrote:
>On Sat, 28 Jun 2014 10:45:03 +0200, Laurent Jumet wrote:
>>
>> 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

unread,
Jun 28, 2014, 12:48:48 PM6/28/14
to

Hello Kenny !

gaz...@shell.xmission.com (Kenny McCormack) wrote:

>>> 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

Stan Brown

unread,
Jun 28, 2014, 4:57:13 PM6/28/14
to
On Sat, 28 Jun 2014 15:33:29 +0000 (UTC), Kenny McCormack wrote:
> In article <MPG.2e18a91f5...@news.individual.net>,
> Stan Brown <the_sta...@fastmail.fm> wrote:

> >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.

Kenny McCormack

unread,
Jun 28, 2014, 6:20:33 PM6/28/14
to
In article <MPG.2e18f6305...@news.individual.net>,
Stan Brown <the_sta...@fastmail.fm> wrote:
...
>> >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.

>> 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

unread,
Jun 28, 2014, 8:28:54 PM6/28/14
to
On Sat, 28 Jun 2014 22:20:33 +0000 (UTC), gaz...@shell.xmission.com
(Kenny McCormack) wrote in <lonf3h$2c9$1...@news.xmission.com>

>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

unread,
Jun 28, 2014, 9:20:11 PM6/28/14
to
In article <9bnuq9d3qlsv1lvfk...@4ax.com>,
CRNG <sp...@uce.gov> wrote:
>On Sat, 28 Jun 2014 22:20:33 +0000 (UTC), gaz...@shell.xmission.com
>(Kenny McCormack) wrote in <lonf3h$2c9$1...@news.xmission.com>
>
>>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

unread,
Jun 29, 2014, 5:23:47 AM6/29/14
to
On Sun, 29 Jun 2014 01:20:11 +0000 (UTC), gaz...@shell.xmission.com
(Kenny McCormack) wrote in <lonpkb$96l$2...@news.xmission.com>
You post untested crap that doesn't work, therefore bad assumptions or
you are a moron.

Klaus Meinhard

unread,
Jun 29, 2014, 6:34:42 AM6/29/14
to
Hallo 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

unread,
Jun 29, 2014, 10:12:58 AM6/29/14
to

Hello Klaus !

Klaus Meinhard <k_mei...@gmx.de> wrote:

>> 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.

This works at the command line or in a batch:

dir /ad/s/b | FOR %i in (@CON) RD %i

> 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.

Axel Berger

unread,
Jun 29, 2014, 10:37:49 AM6/29/14
to
Stan Brown wrote:
> 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

unread,
Jun 29, 2014, 10:38:04 AM6/29/14
to
Stan Brown wrote:
> If you had
> done that -- as I did -- you'd have found that what I said was
> correct.

Message has been deleted

Kenny McCormack

unread,
Jun 29, 2014, 12:02:42 PM6/29/14
to
In article <XnsA35BC0011E...@94.75.214.90>,
hopflop <---@---.--> wrote:
>"Laurent Jumet" <1st_NAME...@skynet.be> typed with big furry
>paws:
>
>> 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

unread,
Jun 29, 2014, 5:04:25 PM6/29/14
to
On Sun, 29 Jun 2014 16:12:58 +0200, Laurent Jumet wrote:
> This works at the command line or in a batch:
>
> dir /ad/s/b | FOR %i in (@CON) RD %i
>

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

unread,
Jun 29, 2014, 5:07:19 PM6/29/14
to
On Sun, 29 Jun 2014 15:52:22 +0000 (UTC), hopflop wrote:
> "Laurent Jumet" <1st_NAME...@skynet.be> typed with big furry
> paws:
>
> > [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.
0 new messages