Sunday 29 December 2013

Reading files recursively from a directory in Perl

Here's a sample illustrative script to read filenames from within a directory recursively in Perl and then executing some commands is as below:

use strict;
use warnings;

my $dir = "/usr/var/";   <----- here you can mention the location / path of the directory

opendir DIR, $dir;   <---- DIR is the directory handler

my @files = readdir DIR;

foreach my $file(@files) {

// execute_your_own_commands_here

}

closedir DIR;

As you see above, the array @files will store all the filenames within the specified directory.
In the next foreach loop, we perform some set of commands recursively on each of the files.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker