Recursively run another script on every file in a directory structure. I know you can do the same thing with the "find" command, but the arguments required make my head hurt...

Usage:
(first, name it recurse.pl and put it in a directory in your PATH)
recurse.pl /complete/path/to/other/script.pl dirname
or, if you need arguments to your script
recurse.pl "/complete/path/to/other/script.pl -blah" dirname
might work (amazing that I haven't tested that).

Assumption:
The other script returns a 0 if it succeeds and something else if it fails.
#!/usr/bin/perl #this needs some comments... chdir $ARGV[1]; @ls_retval = `ls -l`; shift ( @ls_retval ); foreach $line (@ls_retval) { @items = split( /\s+/, $line ); $first_char = substr( $items[0], 0, 1 ); if ( $first_char eq 'd' ) { print "directory: $items[8]\n"; print `$0 $ARGV[0] $items[8]`; } elsif ( $first_char eq 'l' ) { print "link: $items[8]\n"; print `$0 $ARGV[0] $items[8]`; } elsif ( $first_char eq '-' ) { print "file: $items[8]\n"; $action_retval = `$ARGV[0] $items[8]`; if ( $action_retval != 0 ) { print "Error $action_retval in $items[8]\n"; } else { print "OK $action_retval for $items[8]\n"; } } } print "Finished\n";

Replies are listed 'Best First'.
Re: Recursively run another script
by Aristotle (Chancellor) on Aug 08, 2003 at 00:54 UTC
    use strict; use File::Find; my $action = shift; find(sub { return unless -f; system($ARGV[0], $_) == 0 or warn "Non-zero exit code for $_"; }, @ARGV);

    Makeshifts last the longest.

      Ok. Um - yes. That is *way* more elegant than my code.

      However, my code has no dependencies - which is important to me, at least, because (for reasons I'd rather not go into because they're bad for my blood-pressure) I can't use modules at work, which is where I use the code.
        File::Find has been in the Perl core distribution forever. That's not a dependency any more than requiring a perl interpreter is a dependency.

        Makeshifts last the longest.

        You code is quite dependant on os. If you wanted to avoid file::find you should at least use opendir to get your file list. But really in the end you'll be much better off if you get comfy with file::find