This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Webmaster forum > August 2005 > Useful script perhaps





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Useful script perhaps
Toby Inkster

2005-08-28, 4:22 am

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;
}
===================================================

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews