Wednesday, June 15, 2005

Perl Script: directory file manip

To perform some actions on all files in a directory
#!/usr/bin/perl

$input_directory = ".";
chomp $input_directory;

until (-d $input_directory) { # the -d bit means check if there is a directory called ...
print "Sorry, that path / directory does not appear to exist, please try again.\n";
$input_directory = ;
chomp $input_directory;
}


opendir (INPUTDIR, $input_directory) || die "sorry, system cannot open $input_directory $input_directory";

while ($infile = readdir(INPUTDIR)) {
chomp $infile;
if ($infile !~ /^\./) {
if ($infile =~ /.*pgm/) {
$outfile = substr($infile,0,length($infile)-4);
print "\n convert $infile $outfile.jpg";
system("\n convert $infile $outfile.jpg");
}
}
}
closedir (INPUTDIR);

No comments:

Post a Comment