Useful script perhaps

T

Toby Inkster

Perl script below runs a particular command on multiple files.

I use this script when I've written a bunch of PHP pages that I need to
upload to a server with no PHP support. I create the PHP pages using
".html" extensions and then run:

folder.pl 'php {} >../{}' *.html

Here is the script:

==================== folder.pl ====================
#!/usr/bin/perl

use Getopt::Long qw(GetOptions);
my $VERSION = '1.1';
my $opts;

GetOptions
(
"usage|help|h" => \$opts{'help'},
"version" => \$opts{'version'},
"verbose|v+" => \$opts{'verbose'}
);

if ($opts{'version'}==1)
{
print "$VERSION\n";
}

&errormessage if ($opts{'help'}==1);

$cmd = shift @ARGV || &errormessage;
$nfiles = 0;

while ($_ = shift @ARGV)
{
$nfiles++;
$c = $cmd;
$c =~ s/\{\}/$_/g;
print '$ ' . $c . "\n"
if ($opts{'verbose'}==1);
system($c);
}


if ($nfiles==0)
{
die "Must specify some files!\n"
}
else
{
print "$nfiles files processed.\n"
if ($opts{'verbose'}==1);
}


sub errormessage
{
print "\n";
print "folder.pl - folds a command onto multiple files\n";
print "Version $VERSION (c) 2005 Toby Inkster\n";
print "License: http://www.gnu.org/copyleft/gpl.html\n\n";
print "USAGE: folder.pl [options] command file [file...]\n\n";
print " \"command\" is a command to run on each file. Use {} to refer to the\n";
print " filename (much like the \"find\" command). You may find it benificial\n";
print " to use \'single quotes\' around the command.\n\n";
print " Many UNIX shells provide similar built-in functionality, but folder.pl\n";
print " has an easier syntax.\n\n";
print "OPTIONS:\n";
print " --verbose -v Be verbose.\n";
print " --usage --help -h Display help message.\n";
print " --version Display version.\n\n";
print "EXAMPLES:\n";
print " folder.pl \'php {} >../{}.html\' *.php\n";
print " folder.pl \'mv {} {}l\' *.htm\n";
print "\n";
exit;
}
===================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top