收录日期:2019/10/22 15:11:01 时间:2009-11-11 17:59:37 标签:perl,command-line

How do I run a Perl script on multiple input files with the same extension?

 perl scriptname.pl file.aspx

I'm looking to have it run for all aspx files in the current directory

Thanks!

In your Perl file,

 my @files = <*.aspx>;
 for $file (@files) {

      # do something.

 }

The <*.aspx> is called a glob.

you can pass those files to perl with wildcard

in your script

foreach (@ARGV){
    print "file: $_\n";
    # open your file here...
       #..do something
    # close your file
}

on command line

$ perl myscript.pl *.aspx

You can use glob explicitly, to use shell parameters without depending to much on the shell behaviour.

for my $file ( map {glob($_)} @ARGV ) { 
   print $file, "\n";
};

You may need to control the possibility of a filename duplicate with more than one parameter expanded.

If you are on Linux machine, you could try something like this.

for i in `ls /tmp/*.aspx`; do perl scriptname.pl $i; done

You can also pass the path where you have your aspx files and read them one by one.

#!/usr/bin/perl -w

use strict;

my $path = shift;
my @files = split/\n/, `ls *.aspx`;

foreach my $file (@files) {
        do something...
}