PDA

View Full Version : readdir and chdir problem


Ido Perelmutter
05-22-2003, 03:10 PM
Well I'm back here again. This time, I want to print the files in a certain directory on the server where my program is installed.
I use the variable $cur to hold the absolute path to the directory, and the following code to print files:

opendir (DIR, $cur);
@files = readdir DIR;
closedir DIR;
for $i (@files) {
# Do Something With the files #
}

Well, it doesn't work. If I open "." or ".." instead of $cur it works, so I've tried to change the current working directory to $cur with chdir($cur), but it doesn't work too.

What might be the problem?
thanks.

kencl
05-26-2003, 07:35 AM
The problem is likely the value you have in $cur. What do you have in there? Print it out to make sure it's what you think it is:

print "$cur\n";

One other thing to note. Since the directory you are reading may contain sub-directories, and you only want to deal with files in that directory, you should change the readdir line to:

@files = grep { -f } readdir DIR;

This will ensure that @files only contains files and no sub-directories.

Ido Perelmutter
05-27-2003, 07:16 PM
Originally posted by kencl
The problem is likely the value you have in $cur. What do you have in there? Print it out to make sure it's what you think it is:

print "$cur\n";

One other thing to note. Since the directory you are reading may contain sub-directories, and you only want to deal with files in that directory, you should change the readdir line to:

@files = grep { -f } readdir DIR;

This will ensure that @files only contains files and no sub-directories.

Thank you, the problem was the value in $cur, somehow a blank space filtered its way into $cur after the directory path.
Works good now.

Thanks.