Home > Archive > PERL Beginners > June 2007 > read a list of subdirectories
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 |
read a list of subdirectories
|
|
| Luba Pardo 2007-06-21, 9:58 pm |
| Dear list,
I want to parse through some files of a list of directories. Every directory
have the same files, which means that I can make a loop and repeat the
process for each directory. I managed to write the code to process the files
of a single directory but I do not exaclty how to read a list of directories
and open one by one. I can only print the directories that are in the
parental directory , but I can't make the script to open the directory and
read the list of files. I do not what is the function to use to either open
everydir or read the 'subdirectories'. I hope someone can help
The beginning of the script looks like:
#! /usr/bin/perl
use strict;
use warnings;
my $pwd=$ENV{'PWD'};
my @filedir =<*ctt>;
foreach my $filedir (@filedir) {
opendir ($filedir) || die "can't \n";
my @f = <mlc*>;
foreach my $f(@f) {
open (IN,"$f") or die "$!\n";
print "$f\n";
my @a2= (<IN> );
...
Thanks in advance,
L. Pardo
| |
| Steve Bertrand 2007-06-21, 9:58 pm |
| Luba Pardo wrote:
> Dear list,
> I want to parse through some files of a list of directories. Every
> directory
> have the same files, which means that I can make a loop and repeat the
> process for each directory. I managed to write the code to process the
> files
> of a single directory but I do not exaclty how to read a list of
> directories
> and open one by one. I can only print the directories that are in the
> parental directory , but I can't make the script to open the directory and
> read the list of files. I do not what is the function to use to either open
> everydir or read the 'subdirectories'.
You may find it advantageous to read:
perldoc File::Find
Steve
| |
| Luba Pardo 2007-06-21, 9:58 pm |
| hi,
Thank you very much for your sugestions.
L.Pardo
| |
| John W. Krahn 2007-06-21, 9:58 pm |
| Luba Pardo wrote:
> Dear list,
Hello,
> I want to parse through some files of a list of directories. Every
> directory
> have the same files, which means that I can make a loop and repeat the
> process for each directory. I managed to write the code to process the
> files
> of a single directory but I do not exaclty how to read a list of
> directories
> and open one by one. I can only print the directories that are in the
> parental directory , but I can't make the script to open the directory and
> read the list of files. I do not what is the function to use to either open
> everydir or read the 'subdirectories'. I hope someone can help
> The beginning of the script looks like:
>
> #! /usr/bin/perl
> use strict;
> use warnings;
>
> my $pwd=$ENV{'PWD'};
use Cwd;
my $pwd = cwd;
> my @filedir =<*ctt>;
You are populating @filedir with all entries ending in 'ctt', including those
that are *not* subdirectories.
> foreach my $filedir (@filedir) {
> opendir ($filedir) || die "can't \n";
perldoc -f opendir
You need a directory handle (same as open() needs a filehandle.) You should
also include the $! variable in the error message so you know *why* it failed.
> my @f = <mlc*>;
You are populating @f with all entries beginning in 'mlc' in the *current*
*directory*, not in the $filedir directory.
> foreach my $f(@f) {
> open (IN,"$f") or die "$!\n";
perldoc -q quoting
You are trying to open the file $f in the *current* *directory*, not in the
$filedir directory.
> print "$f\n";
> my @a2= (<IN> );
> ...
You *may* want someting like this (UNTESTED):
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
my $pwd = cwd;
for my $filedir ( grep -d, <*ctt> ) {
for my $f ( grep -f, <$filedir/mlc*> ) {
open my $IN, '<', $f or die "Cannot open '$f' $!\n";
print "$f\n";
my @a2 = <$IN>;
...
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Paul Lalli 2007-06-21, 9:58 pm |
| On Jun 21, 8:15 am, lubapa...@gmail.com (Luba Pardo) wrote:
> I want to parse through some files of a list of directories. Every directory
> have the same files, which means that I can make a loop and repeat the
> process for each directory. I managed to write the code to process the files
> of a single directory but I do not exaclty how to read a list of directories
> and open one by one. I can only print the directories that are in the
> parental directory , but I can't make the script to open the directory and
> read the list of files. I do not what is the function to use to either open
> everydir or read the 'subdirectories'.
I can't parse this sentence. If you meant "I do not know what is the
function", then you want the File::Find module. See `perldoc
File::Find`.
If you meant "I do not want to read the subdirectories", then use the
filetest operators (`perldoc -f -X`) to determine whether each entry
is a file or a directory, and skip over the directories.
> I hope someone can help
> The beginning of the script looks like:
>
> #! /usr/bin/perl
> use strict;
> use warnings;
>
> my $pwd=$ENV{'PWD'};
> my @filedir =<*ctt>;
> foreach my $filedir (@filedir) {
> opendir ($filedir) || die "can't \n";
> my @f = <mlc*>;
This does not do what you think it does. opendir() only opens a
directory for reading via readdir(). It does not change the current
working directory of your script. You have two options:
1) Change working directory, and then ask the shell for all files in
the current working directory
2) Open a directory, and read the files in that directory.
You, however, are opening a directory, and then asking the shell for
all the files in the current working directory.
Change these two lines to either:
opendir my $dh, $filedir or die $!
my @f = grep /^mlc/, readdir($dh);
or
chdir $filedir or die $!
my @f = <mlc*>;
(You were also calling opendir() without a directory handle argument -
I'm suprised that didn't give you a syntax error).
See also:
perldoc -f opendir
perldoc -f chdir
perldoc -f readdir
perldoc -f glob
Hope that helps,
Paul Lalli
|
|
|
|
|