Looping Dir entires with space

E

Eric Anderson

I am developing a Perl CGI application that needs to work on both
Windows and UNIX. In a couple of places in the app, I read a list of
files from a directory. My basic code is something like:

my $config_dir = '/conf/';
if( -d $config_dir ) {
my $file;
while(defined($file=<$config_dir*>)) {
print "$file\n";
}
}

This seems to work just fine. But if the variable $config_dir has a
string with a space in it (e.g. '/Program Files/Foo Bar') then it
doesn't seem to list the files correctly. The app seems to be reading
each part of the path (split on the space) as it's own file. So my
output will be something like:

/Program
Files/Foo

So my first thought is to change the code to the following:

my $config_dir = '/Program Files/';
if( -d $config_dir ) {
my $file;
while(defined($file=<"$config_dir"*>)) {
print "$file\n";
}
}

This makes it work fine with there is a space in the path but now it
doesn't work when there is not a space in the path. If $config_dir is
set to '/conf/' I don't get a listing at all.

So my question is how do I iterate through a list of files in a
directory in such a way that will work on both UNIX and Windows and work
when the path has a space and doesn't have a space. Also sometimes I
will need to do stuff like:

while(defined($file=<$config_dir/foo/bar/*>)) {
print "$file\n";
}

I need this to work when $config_dir has a space and doesn't have a
space. I checked google and the various perl man pages but wasn't able
to find the answer I am looking for. Any help would be greatly appreciated.

Eric
 
E

Eric Anderson

Eric said:
So my question is how do I iterate through a list of files in a
directory in such a way that will work on both UNIX and Windows and work
when the path has a space and doesn't have a space.

Nevermind. After some additional searching I found the following method:

opendir(DIR,$config_dir);
foreach my $file ( readdir(DIR) ) {
print "$file\n";
}
closedir(DIR);

Thanks anyway,

Eric
 
G

Gunnar Hjalmarsson

Eric said:
I am developing a Perl CGI application that needs to work on both
Windows and UNIX. In a couple of places in the app, I read a list of
files from a directory. My basic code is something like:

my $config_dir = '/conf/';
if( -d $config_dir ) {
my $file;
while(defined($file=<$config_dir*>)) {
print "$file\n";
}
}

This seems to work just fine. But if the variable $config_dir has a
string with a space in it (e.g. '/Program Files/Foo Bar') then it
doesn't seem to list the files correctly.

So my question is how do I iterate through a list of files in a
directory in such a way that will work on both UNIX and Windows and work
when the path has a space and doesn't have a space.

You can escape the spaces before the while loop:

$config_dir =~ s/ /\\ /g;

I'm still not sure about the portability of the <dir/*> notation, and
usually I use opendir() and readdir() when I need portable code.
 
T

Tad McClellan

Eric Anderson said:
I am developing a Perl CGI application that needs to work on both
Windows and UNIX.

while(defined($file=<$config_dir*>)) {

This seems to work just fine. But if the variable $config_dir has a
string with a space in it (e.g. '/Program Files/Foo Bar') then it
doesn't seem to list the files correctly. The app seems to be reading
each part of the path (split on the space) as it's own file.


Spliting on spaces is what globbing is _supposed_ to do.

So my first thought is to change the code to the following:


[snip more glob()ing]

So my question is how do I iterate through a list of files in a
directory in such a way that will work on both UNIX and Windows and work
when the path has a space and doesn't have a space.


Don't use globbing, dealing with quoting and backslashing
and whatnot just isn't worth it.

Use these instead:

perldoc -f opendir
perldoc -f readdir
perldoc -f closedir

Also sometimes I
will need to do stuff like:

while(defined($file=<$config_dir/foo/bar/*>)) {
print "$file\n";
}


The equivalent (kinda) without globbing (untested):

my $dir = "$config_dir/temp/benchmarks";
opendir DIR, $dir or die "could not open '$dir' $!";
foreach my $file ( sort grep /^[^.]/, readdir DIR) {
print "$dir/$file\n";
}
closedir DIR;
 

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
474,166
Messages
2,570,907
Members
47,447
Latest member
TamiLai26

Latest Threads

Top