How can I recursively read out directories in Perl?

前端 未结 6 2415
一生所求
一生所求 2021-02-14 06:08

I want to read out a directory recursively to print the data-structure in an HTML-Page with Template::Toolkit. But I\'m hanging in how to save the Paths and Files in a form that

6条回答
  •  太阳男子
    2021-02-14 06:39

    I use this script to remove hidden files (created by Mac OS X) from my USB Pendrive, where I usually use it to listen music in the car, and any file ending with ".mp3", even when it starts with "._", will be listed in the car audio list.

    #!/bin/perl
    
    use strict;
    use warnings;
    
    use File::Find qw(find);
    
    sub list_dirs {
            my @dirs = @_;
            my @files;
            find({ wanted => sub { push @files, $_ } , no_chdir => 1 }, @dirs);
            return @files;
    }
    
    if ( ! @ARGV || !$ARGV[0] ) {
      print "** Invalid dir!\n";
      exit ;
    }
    
    
    if ( $ARGV[0] !~ /\/Volumes\/\w/s ) {
      print "** Dir should be at /Volume/... > $ARGV[0]\n";
      exit ;
    }
    
    my @paths = list_dirs($ARGV[0]) ;
    
    foreach my $file (@paths) {
      my ($filename) = ( $file =~ /([^\\\/]+)$/s ) ;
    
      if ($filename =~ /^\._/s ) {
        unlink $file ;
        print "rm> $file\n" ;
      }
    }
    

提交回复
热议问题