drum1981 has asked for the wisdom of the Perl Monks concerning the following question:

hi everyone. i am working with a script whose duties are to transfer some subdirectories from one location to another, along with the respective contents. the problem i have come across is that random files were left behind of various extentions( maybe .dll's, .kex's, and .sea's to just name a couple. mostly they are random extensions that i have never heard of). can anybody suggest an efficient way to after moving the contents of a subdirectory, to make sure that it has fully moved all the contents. right now after the script finishes, it says everything has moved even though there are still these random files. so basically, can anyone suggest something that makes sure a subdirectory is empty, and if not, moves whatever is there? -thanks
  • Comment on checking to see if a directory is empty

Replies are listed 'Best First'.
Re: checking to see if a directory is empty
by cmeyer (Pilgrim) on Jul 27, 2005 at 18:27 UTC

    How about using opendir() and readdir()?

    my $dir = '/tmp/foo'; if ( my @contents = dir_contents( $dir ) { do_something_about_it( $dir, @contents ); } else { it_is_empty( $dir ) } sub dir_contents { my $dir = shift; opendir DIR, $dir or die "Couldn't open $dir: $!"; return grep !/^\.\.?$/, readdir DIR; }

    -Colin.

    WHITEPAGES.COM | INC

Re: checking to see if a directory is empty
by friedo (Prior) on Jul 27, 2005 at 18:25 UTC
    You could go globbing:

    my $num; chdir $dir_to_check; $num++ while <*>; print "There are still files in $dir_to_check\n" if $num;

    Update Used a better glob.

Re: checking to see if a directory is empty
by bofh_of_oz (Hermit) on Jul 27, 2005 at 19:16 UTC
    I would suggest using File::Copy::Recursive to transfer directories (or File::NCopy if you wish) recursively, File::Dircmp to verify if everything was copied properly, and File::Path to remove source tree.

    And I also know that there should be an easier way to do it...

    --------------------------------
    An idea is not responsible for the people who believe in it...

Re: checking to see if a directory is empty
by datmrman (Initiate) on Jul 27, 2005 at 19:04 UTC
    The rmdir FILENAME command will only delete directories that are empty. If it succeeds it returns true, otherwise it returns false and sets $! (errno). Maybe you can look into using that as a flag?
Re: checking to see if a directory is empty
by Fletch (Bishop) on Jul 27, 2005 at 20:50 UTC

    Rather than iterating you could use stat to look at the nlink field; on a *NIX system an empty directory should have exactly two links (one from its parent to itself, and one from its own . entry).

    --
    We're looking for people in ATL

      Note that some *NIX flavors do allow hard links to directories! It is admittedly highly unlikely that your friendly sysadmin would dare to use this privilege, for fear of corrupting the file system.

      However, if she did, that directory would have a higher linkcount than expected, even when devoid of subdirectories.

      Also note that files contained in a directory do not count towards the directory's link-count. Only immediate subdirectories do.

Re: checking to see if a directory is empty
by holli (Abbot) on Jul 27, 2005 at 19:16 UTC
    What function/command do you use to do the move operation? And, more important, do you check the return value of that function/command?


    holli, /regexed monk/
Re: checking to see if a directory is empty
by sh1tn (Priest) on Jul 27, 2005 at 21:08 UTC
    unless( glob "*" ){ print "empty dir\n" }else{ print "non-empty dir\n" }