Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

if (-d$_) failes because of white space

by Gretter (Novice)
on May 13, 2009 at 11:13 UTC ( #763730=perlquestion: print w/ replies, xml ) Need Help??
Gretter has asked for the wisdom of the Perl Monks concerning the following question:

Some sub-directories and files have white spaces in their names. The following test failes on those:
foreach(@dir) { if (-d$_) {print $_," is a directory.\n"; }
What can I do to find the sub-directories and subsequently open them, even when they have white spaces?

Problem solved:
foreach(@dir) { my $subdir = $dir.$_; if (-d$_) {print $_," is a directory.\n"; }
The solution was to specify the entire path.

Comment on if (-d$_) failes because of white space
Select or Download Code
Re: if (-d$_) failes because of white space
by Anonymous Monk on May 13, 2009 at 11:18 UTC
    You are misdiagnosing the problem, because whitespace isn't a problem.
    #!/usr/bin/perl -- mkdir 'i got white space'; mkdir 'i got white space/one'; mkdir 'i got white space/o n e'; mkdir 'i got white space/o ne'; mkdir 'i got white space/one'; opendir INDIR, 'i got white space'; print "($_)\n" for readdir INDIR; closedir INDIR; __END__ (.) (..) (o n e) (o ne) (one)
Re: if (-d$_) failes because of white space
by cdarke (Prior) on May 13, 2009 at 11:35 UTC
    Works for me, are you sure you are not passing these names through the command-line (@ARGV) and missing quotes? Or maybe you are using < > globbing?
Re: if (-d$_) failes because of white space
by Utilitarian (Vicar) on May 13, 2009 at 12:07 UTC
    Oh no it doesn't !
    mkdir test\ this perl -e 'opendir (DIR,"./");@files = readdir DIR;foreach $file (@files +){print "$file is a directory\n" if (-d $file);}' test this is a directory .. is a directory . is a directory
    Could you provide a wider picture of the issue, perhaps an ls -lR of the directory structure and a larger code snippet?
Re: if (-d$_) failes because of white space
by leocharre (Priest) on May 13, 2009 at 15:09 UTC
    Your problem may be that your $_ does NOT contain full path to the item in question.

    Change your code to die() if not a dir and tell you what it tested for.

    use Cwd; foreach(@dir) { if (-d$_) { print $_," is a directory.\n"; } else { die("Not a dir '$_', current working directory is: ". cwd()) +; } }
Re: if (-d$_) failes because of white space
by radiantmatrix (Parson) on May 13, 2009 at 15:53 UTC

    You need some diagnostics, friend (and code tags...):

    use strict; use warnings; use Data::Dumper::Simple; foreach my $d (@dir) { if (-d $d) { print $d, "is a directory.\n"; } else { warn "Not a directory:".Dumper(\$d); } }

    Try that and let us see your output. FWIW, you should also take a moment and read How (Not) To Ask A Question to help you get better answers faster.

    <radiant.matrix>
    Ramblings and references
    “A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” Herm Albright
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: if (-d$_) failes because of white space
by Gretter (Novice) on May 13, 2009 at 15:54 UTC
    In bash i do: export ril=~/.mozilla/firefox/profiledirectory/Read-It-Later\ Offline/ +f echo $ril /home/usrname/.mozilla/firefox/profiledirectory/Read-It-Later Offline/ I call the script like this: myscript.pl "$ril" this is the script: #!/usr/bin/perl -w $dir = shift or die("Use directory as argument.\n"); print $dir,"\n"; opendir DIR,$dir or die "Can't open directory.\n"; @dir = readdir DIR; closedir DIR; foreach(@dir) { if (-d$_) {print $_," is a directory.\n";} elsif (-f$_) {print $_," is a file.\n";} else {print $_," is not a file or a directory!\n";} } this is the out put of the script: /home/usrname/.mozilla/firefox/profiledirectory/Read-It-Later Offline/ . is a directory. .. is a directory. RIL 533_files is not a file or a directory! list_of_reports.html is a file. RIL 533.html is not a file or a directory! RIL 1178.html is not a file or a directory! RIL 1178_files is not a file or a directory!
      opendir DIR,$dir or die "Can't open directory.\n"; @dir = readdir DIR; ... foreach(@dir) { if (-d$_) {print $_," is a directory.\n";} elsif (-f$_) {print $_," is a file.\n";} else {print $_," is not a file or a directory!\n";} }

      readdir() gives you directory entries without the path component. So you have to either chdir() into the directory before your foreach loop, or write

      foreach(@dir) { my $path = "$dir/$_"; # prepend dir if (-d $path) {print $path," is a directory.\n";} elsif (-f $path) {print $path," is a file.\n";} else {print $path," is not a file or a directory!\n";} }
Re: if (-d$_) failes because of white space
by ikegami (Pope) on May 13, 2009 at 16:08 UTC

    Not enough info. There are two likely possibilities (neither of which have anything to do with -d).

    Are you using glob without realizing that spaces are metacharacters for glob? Use File::Glob's bsd_glob instead. It's the same function, but it treats spaces as "ordinary" characters.

    Are you using the result of readdir (an unqualified file name) without prepending the path of that file name?

Re: if (-d$_) failes because of white space
by nimdokk (Vicar) on May 13, 2009 at 18:44 UTC
    Try wrapping quotes around $_.

    my $dir = "\'$_\'";

    Some OS platforms don't like those spaces (even tho it allows you to put them into the directory names *cough*windows*cough* :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://763730]
Approved by Gangabass
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-22 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (126 votes), past polls