473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Jul 19 '05 #1
2 8903
Matt wrote:
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";
}
}
Jul 19 '05 #2
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
"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:5L9td.754$zh7.152@trnddc02...
Matt wrote:
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";
}
}

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oliver | last post by:
Hi, I wrote earlier this day about this problem but thought it had something to do with images. However, I found out that for some reason the readdir function is not working properly on my Suse...
7
by: Ask Josephsen | last post by:
Hi NG In wich order does "readdir()" read files from the disc? I've got an image folder with images in the format "00001.jpg", "00002.jpg" etc. It seems "readdir()" read the lowest first, but is...
1
by: Scott Medaugh | last post by:
Hello, I am trying to do something unusual and it has me stumped. I am looking to change the Env variable for RemoteUser essentially. What I would like to happen is that the user would choose...
4
by: FayeC | last post by:
I have tried to use a php code (found it online) to create a gallery but I am wondering if thereare any other PHP options besides using EXIF. The reason is that the images I am using for the...
3
by: Eric Capps | last post by:
I am trying to open a directory on a file server to populate a drop down menu. I've been able to do this, but the results are not sorted alphabetically. How would I go about this? I've looked at...
4
by: Sonnich | last post by:
Hi all I was wondering to try something like this: exec("dir \"".$sSearchPath."\\*.pdf\" >c:\hhh.txt"); though, it hangs at that point. I tried system(), shell_exec/() etc similar result. ...
9
by: Confused but working on it | last post by:
Just wanted to say thanks for the posts helping me make ths work. Added a few comments and after the readdir used a pattern match to test for ..jpg, and seems to work fine on my test setup. Maybe I...
5
crystal2005
by: crystal2005 | last post by:
Hi all, My short program below is to read directories names form command line. It should only start with alphanumeric characters and contain no other characters except spaces, underscores and...
1
by: rahullko05 | last post by:
I am try to write a program which can recursively traverse all files and if it finds directory then it goes inside that sub directory and list all files of that subdirectory, again comes back to main...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.