in reply to (golf) Recursively delete empty directories from a tree

How about this one? It could be even shorter if you remove the warning.
#!/usr/bin/perl use File::Find; finddepth sub { return if $_ eq "." or $_ eq ".."; return unless -d; rmdir($_); if($! =~ /not empty/){warn "$File::Find::name $!"} }, @ARGV;
UPDATE:
#!/usr/bin/perl use File::Find; finddepth sub { return if (! -d) or ($_ eq '.'|'..'); rmdir($_)}, @ARGV;

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Re: (golf) Recursively delete empty directories from a tree
by greenFox (Vicar) on Feb 18, 2004 at 23:30 UTC
    Your solutions don't meet demerphq's specification for printing out a tally of directories kept or removed... I made it shorter any-way :)
    use File::Find; finddepth sub {(!-d or /^.{1,2}$/)||rmdir($_)},@ARGV;
    I wonder what else a day that begins with me posting code to a golf thread will have in store *grin*

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

      finddepth sub {(!-d or /^.{1,2}$/)||rmdir($_)},@ARGV;
      didn't run for me, I needed to separate the rmdir with ;
      use File::Find; finddepth sub {(!-d or /^.{1,2}$/);rmdir},@ARGV;
      or with a print
      use File::Find; finddepth sub {(!-d or /^.{1,2}$/);rmdir; warn $File::Find::name,$! if $! =~ /not empty/},@ARGV;

      I'm not really a human, but I play one on earth. flash japh

        finddepth sub {(!-d or /^.{1,2}$/);rmdir},@ARGV;

        That makes the test a seperate statement from the action (rmdir). So the test is doing nothing and you are running rmdir on everything, which fails silently for files and non empty directories. Change rmdir to print to see it, thats how I tested my version.

        No idea why my version won't work for you it runs OK for me :)

        --
        Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho