06-01-2011

Perl Directory file loop

This Perl script loops through the files in the specified directory:

#! /usr/bin/perl
 
use strict;
use warnings;
 
my $d = "./images";
 
opendir(D, "$d") || die "Can't open dir $d: $!\n";
my @list = readdir(D);
closedir(D);
 
foreach my $f (@list) {
  print "$f\n";
}
 

Or just use perl's glob function: (http://perl.about.com/od/filesystem/qt/perlglob.htm)

 
 @files = <./images/*>;
 foreach $file (@files) {
   print $file . "\n";
 }

To just open a file and loop through it's lines:

open (MYFILE, 'data.txt');
 while (<MYFILE>) {
 	chomp;
 	print "$_\n";
 }
 close (MYFILE);
 

Comments:

Your comment:

»

 

[x]