chdir and directory name with whitespace

A

als

Hi,
I have a problem with chdir function when the name of a directory have
a whitespace. Ex. "/zzz/yyy/new york"
The chdir,in this case, it doesn't work.
The path is not constant but I get this with a list of directories from
one root.

Thanks.
 
P

Paul Lalli

als said:
I have a problem with chdir function when the name of a directory have
a whitespace. Ex. "/zzz/yyy/new york"
The chdir,in this case, it doesn't work.

"does not work" is the worst possible of all error descriptions. What
actually happens? What's the error message?

Have you read the Posting Guidelines for this group?

$ mkdir foo
$ mkdir foo/bar
$ mkdir "foo/bar/new york"
$ perl -e'chdir "foo/bar/new york" or die $!; print `pwd`; '
/home/mritty/foo/bar/new york
$

Seems to work pretty well for me.... Perhaps you should follow the
Posting Guidelines and post a short-but-complete script that
demonstrates your problem.

Paul Lalli
 
G

Gunnar Hjalmarsson

als said:
I have a problem with chdir function when the name of a directory have
a whitespace. Ex. "/zzz/yyy/new york"
The chdir,in this case, it doesn't work.

Would you mind showing us a short but complete program that demonstrates
the problem?
 
B

Babacio

als said:
Hi,
I have a problem with chdir function when the name of a directory have
a whitespace. Ex. "/zzz/yyy/new york"
The chdir,in this case, it doesn't work.

It should work. Which system do you use ?
The path is not constant but I get this with a list of directories
from one root.

Is it a relevant information ? More precisely : does this mean that
you can't reproduce the bug with a constant path ?
 
A

als

Hi and thanks all,
more in deep (it's only an example):

opendir(MYDIR, $rootdir);
my (@files) = readdir MYDIR;
foreach $subdir (@files) {
if ($subdir ne '.' && $subdir ne '..' && (-d "$rootdir/$subdir")) {
chdir ("$rootdir/$subdir") or die "WHITESPACE";
}
}

yes, it's mean that I can't reproduce the bug with a constant path.
 
G

Gunnar Hjalmarsson

als said:
opendir(MYDIR, $rootdir);
my (@files) = readdir MYDIR;
foreach $subdir (@files) {
if ($subdir ne '.' && $subdir ne '..' && (-d "$rootdir/$subdir")) {
chdir ("$rootdir/$subdir") or die "WHITESPACE";
----------------^^^^^^^^^^^^^^^^

One thought is that you'd better use File::Spec for a safer way to
concatenate into paths.

use File::Spec;
opendir MYDIR, $rootdir or die "Couldn't open $rootdir: $!";
my @files = readdir MYDIR;
foreach my $subdir (@files) {
my $path = File::Spec->catfile($rootdir, $subdir);
if ($subdir ne '.' && $subdir ne '..' && -d $path) {
chdir $path or die "WHITESPACE";
}
}
closedir MYDIR;
 
P

Paul Lalli

als said:
Hi and thanks all,
more in deep (it's only an example):

Stop doing that. Give a *real* piece of code.
opendir(MYDIR, $rootdir);

You haven't checked to see whether or not this opendir succeeds.

opendir my $MYDIR, $rootdir or die "Cannot open $rootdir: $!";
my (@files) = readdir MYDIR;

(Now that we're using a lexical directory handle, this will need to be
changed too)
my @files = readdir $MYDIR;
foreach $subdir (@files) {
if ($subdir ne '.' && $subdir ne '..' && (-d "$rootdir/$subdir")) {
chdir ("$rootdir/$subdir") or die "WHITESPACE";

Why aren't you asking Perl *why* the changedir failed? Include $! in
your error output.
chdir "$rootdir/$subdir" or die "Cannot open '$rootdir/$subdir': $!";
}
}

yes, it's mean that I can't reproduce the bug with a constant path.

"You're doing something wrong." But you still haven't given us enough
information to actually see what that might be, and you haven't asked
Perl for all the help it will give you.

You *are* using strict & warnings too, right?

Paul Lalli
 
B

Babacio

"als"
yes, it's mean that I can't reproduce the bug with a constant path.

So the bug is somewhere in the generation of the content of variable.
Can you reproduce the bug with something like

$path="/fou/barre/nouvelle orleans/";
chdir($path);

I'll bet you can't. If you can, tell us.

Your piece of code is not what we need. Please try to produce a piece of
code, with minimal size, which produces the problem.

Well, before doing that, you may try to replaxe your 'die
"WHITESPACE"' by a something like
die "Problem with '$rootdir/$subdir' : $!";

which may help you to discover yourself where is the problem.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top