Question about using readdir to read the files in a directory ...

M

Matt

Hello,

I am writing a script that opens needs to get a listing of files in a
directory, print that listing to a file and use that file as a quasi ftp
control file. My problem is that when I print the filenames found via the
readdir I also get the . and .. (current directory and parent directory)
written in the ftp control file which causes my script to stop since you
can't ftp . nor ..

Here's my logic ....


opendir(DON,"/don") || die "Can't opendir /don: $!";
open(DATFILELIST,">>/data/datafiles.out") || die "Can't opendir
/data/datafiles.out: $!";
print DATFILELIST "cd /incoming\n";
print DATFILELIST "lcd /data/utech/dat\n";
while ($donefile = readdir(DON)) {
$datafile = $donefile;
$datafile =~ s/\.don/\.dat/;
print DATFILELIST "get $datafile\n";
}
print DATFILELIST "bye\n";
close(DATFILELIST) || die "Can't close file /data/datafiles.out $!";
closedir(DON) || "Can't closedir /data/utech/don: $!";

The DATFILELIST gets the right files, but it also gets the . and .. which
causes the ftp to fail.

I wonder if my real problem is with my pattern matching. I want to match
all occurences of a file that ends in .don and rename the extension to .dat
then write that output to the DATFILELIST file. This works, but it also
includes the . and ..

Any ideas woud be much appreciated.


Thanks,


Matt
 
J

Jürgen Exner

Matt said:
Hello,

I am writing a script that opens needs to get a listing of files in a
directory, print that listing to a file and use that file as a quasi
ftp control file. My problem is that when I print the filenames
found via the readdir I also get the . and .. (current directory and
parent directory) written in the ftp control file which causes my
script to stop since you can't ftp . nor ..

Here's my logic ....


opendir(DON,"/don") || die "Can't opendir /don: $!";
open(DATFILELIST,">>/data/datafiles.out") || die "Can't opendir
/data/datafiles.out: $!";
print DATFILELIST "cd /incoming\n";
print DATFILELIST "lcd /data/utech/dat\n";
while ($donefile = readdir(DON)) {
$datafile = $donefile;
$datafile =~ s/\.don/\.dat/;
print DATFILELIST "get $datafile\n";
}
print DATFILELIST "bye\n";
close(DATFILELIST) || die "Can't close file /data/datafiles.out $!";
closedir(DON) || "Can't closedir /data/utech/don: $!";

The DATFILELIST gets the right files, but it also gets the . and ..

Not very surprising. Those are directory entires after all, too, and you
don't remove/skip them anywhere.
which causes the ftp to fail.

I wonder if my real problem is with my pattern matching. I want to
match all occurences of a file that ends in .don and rename the
extension to .dat then write that output to the DATFILELIST file.

Actually the pattern match is only the first step in the substitute
operator.
You got a minor issue in the replacement string. The replacement string is a
string, not an RE. Therefore there is no reason to escape the dot.
And you got a maybe significant issue in the RE. Your RE will match anywhere
in the string. If you want to match only the final extension then you must
anchor your RE: /\.don$/
This works, but it also includes the . and ..

Of course. Just as it will contain any file named any other way. Just try
creating some junk files with random names.
Any ideas woud be much appreciated.

If you want to print only specific files, then you may want to use a
condition, e.g. (untested):

if ($datafile =~ s/\.don/\.dat/) {
print DATFILELIST "get $datafile\n";
}

On the other hand the whole sections begs the question why don't you just
use $_ (agaIn, untested):
while (readdir(DON)) {
if (s/\.don/\.dat/) {
print DATFILELIST "get $_\n";
}
}
 
M

Matt

Jurgen,

Thank you for the response. I did try the expression boundaries earlier but
they didn't seem to help so I kept playing around. I will definitely put
them back since I know it is better coding.

I will also work to implement the condition statement.

Thank you for your assistance.


Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top