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!
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!
For example to handle
perl scriptname.pl *.aspx *.asp
In linux: The shell expands wildcards, so the perl can simply be
Windows doesn't expand wildcards so expand the wildcards in each argument in perl as follows. The for loop then processes each file in the same way as above
For example, this will print the expanded list under Windows
In your Perl file,
The
<*.aspx>
is called a glob.For a simple one-liner with
-n
or-p
, you wantThe
-i~
says to modify each target file in place, and leave the original as a backup with an~
suffix added to the file name. (Omit the suffix to not leave a backup. But if you are still learning or experimenting, that's a bad idea; removing the backups when you're done is a much smaller hassle than restoring the originals from a backup if you mess something up.)If your Perl code is too complex for a one-liner (or just useful enough to be reusable) obviously replace
-e '# your code here'
withscriptname.pl
... though then maybe refactorscriptname.pl
so that it accepts a list of file name arguments, and simply usescriptname.pl *.aspx
to run it on all*.aspx
files in the current directory.If you need to recurse a directory structure and find all files with a particular naming pattern, the
find
utility is useful.If your
find
does not support-exec ... +
try with-exec ... \;
though it will be slower and launch more processes (one per file you find instead of as few as possible to process all the files).To only scan some directories, replace
.
(which names the current directory) with a space-separated list of the directories to examine, or even usefind
to find the directories themselves (and then perhaps explore-execdir
for doing something in each directory thatfind
selects with your complex, intricate, business-critical, maybe secret list offind
option predicates).Maybe also explore
find2perl
to do this directory recursion natively in Perl.If you are on Linux machine, you could try something like this.
You can use
glob
explicitly, to use shell parameters without depending to much on the shell behaviour.You may need to control the possibility of a filename duplicate with more than one parameter expanded.
You can also pass the path where you have your aspx files and read them one by one.