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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: (golf) Recursively delete empty directories from a tree
by greenFox (Vicar) on Feb 19, 2004 at 23:54 UTC

    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

      Well I'm using Perl5.80. I setup a test directory like so:
      /1
      /2
      /3
      /4
      /5--
          \__
             \1
             \2
             \3
               \howdy
      
      Everythings empty except for the empty file 'howdy'. If I run your code from the top with "deldir .":
      use File::Find; finddepth sub {(!-d or /^.{1,2}$/)||rmdir($_); warn $File::Find::name,$! if $! =~ /not empty/},@ARGV;
      I get no output at all and the script dosn't remove anything.

      If I run the following:

      use File::Find; finddepth sub {(!-d or /^.{1,2}$/);rmdir($_); warn $File::Find::name,$! if $! =~ /not empty/},@ARGV;
      Everything gets deleted giving this output:
      ./5/3Directory not empty at ./delete-empty-dirs-recursive1 line 10.
      ./5Directory not empty at ./delete-empty-dirs-recursive1 line 10.
      
      I see what you are saying about the -d test not being performed, but I don't see how your code runs. Maybe you have a magic machine. If you or someone can explain why your code dosn't run on my machine, I would be grateful.

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