[pm-h] File::Find and system

Mark Allen mrallen1 at yahoo.com
Tue Sep 20 15:16:37 PDT 2011


Find::File is definitely more cross platform and tested for edge cases, but something like this works pretty well too.

== SNIP ==

use strict;
use warnings;

my $base = $1 || $ENV{'PWD'};

my @dirs = ( $base );

foreach my $dir ( @dirs ) {
      opendir DIR, $dir;
      while ( my $file = readdir DIR ) {
              if ( -d "$dir/$file" ) {
                  next if ( $file eq "." or $file eq ".." );
                  push @dirs, "$dir/$file";
                  next;
              }

              # do stuff, like maybe
              print "Now processing $dir/$file...";
              `/usr/bin/dos2unix $dir/$file`;
              print "done\n";
      }
      closedir DIR;
}

== SNIP END ==

Mark



________________________________
From: Russell L. Harris <rlharris at oplink.net>
To: houston at pm.org
Sent: Tuesday, September 20, 2011 4:42 PM
Subject: [pm-h] File::Find and system

Running Linux, I need to execute various system utilities (including
"dos2unix") on multiple files in multiple directories.  The file
structure is similar to the following:

    foo/jan/01.txt
    foo/jan/02.txt
    foo/jan/03.txt
    ...
    foo/feb/01.txt
    foo/feb/02.txt
    foo/feb/03.txt
    ...
    foo/dec/01.txt
    foo/dec/02.txt
    foo/dec/03.txt
    ...
    bar/jan/01.txt
    bar/jan/02.txt
    bar/jan/03.txt
    ...
    bar/feb/01.txt
    bar/feb/02.txt
    bar/feb/03.txt
    ...
    bar/dec/01.txt
    bar/dec/02.txt
    bar/dec/03.txt
    ...
    foobar/jan/01.txt
    foobar/jan/02.txt
    foobar/jan/03.txt
    ...
    foobar/feb/01.txt
    foobar/feb/02.txt
    foobar/feb/03.txt
    ...
    foobar/dec/01.txt
    foobar/dec/02.txt
    foobar/dec/03.txt
    ...

Regrettably, not all Linux utilities have a recursive option, and I do
not wish to take the time to re-write and debug functions which
already are available as a standard utilities.

A very tedious approach would be to "cd" to "foo/jan/" and run
"dos2unix *.txt", then "cd" to "foo/feb/" and run "dos2unix *.txt",
etc.

I know that a Perl script can automate the process.  I just discovered
the Perl "File::Find" module and the Perl "system" function, and now I
am perusing the O'Reilly Perl books, trying to understand how to
combine the two into a script.

Afterward, I need to do involved search-and-replace processing on
these files which cannot be handled with system utilities.  I
previously have used Perl scripts for similar tasks, but never on a
multi-level directory.

So, learning how to run system utilities with "File::Find" appears to
me to be the logical first step.

RLH
_______________________________________________
Houston mailing list
Houston at pm.org
http://mail.pm.org/mailman/listinfo/houston
Website: http://houston.pm.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/mailman/private/houston/attachments/20110920/0a61f269/attachment.html>


More information about the Houston mailing list