Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
laziness, impatience, and hubris
 
PerlMonks  

Re: opendir and directories that have spaces in them

by bikeNomad (Priest)
on Aug 09, 2001 at 02:51 UTC ( #103269=note: print w/ replies, xml ) Need Help??


in reply to opendir and directories that have spaces in them

This shouldn't be a problem (at least it isn't on my system). Perhaps you're having a problem with something else in your program. Can you post a short program that shows the problem?


Comment on Re: opendir and directories that have spaces in them
Re: Re: opendir and directories that have spaces in them
by Anonymous Monk on Aug 11, 2001 at 00:58 UTC
    it isn't short but this is it:
    use Cwd; $homedir = getcwd(); @dir = $homedir; $feddir = ""; @allfiles = (); @dirnw = (); @directory = (); @files = (); sub putdir { $directories = ""; $spacefiles = ""; opendir (THISDIR, "$feddir") or die "dying! $feddir: $!"; @allfiles = readdir THISDIR; closedir THISDIR; foreach $a (@allfiles) { chomp $a; if ( $a eq "." || $a eq ".." ) { next; } elsif ( -d $a ) { if ($directories eq "") { $directories = "$a"; } else { $directories = "$directories+" . "$a"; } } else { #it should detect if the file has a space in i +t first $rest = ""; (undef,$rest)=split(/\s+/,$a,2); if ($rest ne "") { if ($spacefiles eq "") { $spacefiles = "$feddir/$a"; } else { $spacefiles = "$spacefiles+" . + "$feddir/$a"; } } } } $rtrn = "$directories%" . "$spacefiles"; return $rtrn; } do { foreach $feddir (@dir) { chomp $feddir; $output = putdir(); ($dirs,$file) = split(/\%/,$output); (@files) = split(/\+/,$file); open (DATABASE, ">>/$homedir/listofiles"); foreach $f (@files) { chomp $f; print (DATABASE "$f\n"); #$g = $f; #$g =~ s/\s+/_/g; #rename $f, $g or die "can't rename! $ +f, $g: $!"; #$g = ""; } close DATABASE; (@directory) = split(/\+/,$dirs); foreach $d (@directory) { chomp $d; (undef,$restodir)=split(/\s+/,$d); if ($restodir ne "") { open (DATABASE2, ">>/$homedir/listodir +s"); print (DATABASE2 "$d\n"); close DATABASE2; #$h = $d; #$h =~ s/\s+/_/g; #rename $d, $h or die "can't rename! $ +d, $h: $!"; #$h = ""; } } push @dirnw, @directory; @directory = (); } @dir = (); @dir = @dirnw; @dirnw = (); } until (@dir == "")
      I'm not sure why you're doing a few things there:
      • taking a list and returning it spliced together as a string, only to split it again (why not return two array refs instead?)
      • doing a split just to find out if there's a space character in a filename (why not just use m// ?)
      • chomping filenames from readdir (where would the newlines have come from?)
      • using @dir == "" rather than just @dir to detect whether an array is empty
      • what does the code do when you have a file or directory name with a '+' or '%' character in it?
      • re-opening your database for each file/directory

      What's wrong with something like:

      use File::Find; use Cwd; my $homedir = getcwd(); open DATABASE, ">>$homedir/listofiles" or die "can't open listofiles: + $!\n"; open DATABASE2, ">>$homedir/listodirs" or die "can't open listodirs: $ +!\n"; File::Find::find( sub { return if /^\.\.?$/; return unless /\s/; if ( -d $_ ) { print DATABASE2 "$File::Find::name\n"; } else { print DATABASE "$File::Find::name\n"; } ( my $newName = $File::Find::name ) =~ s/\s+/_/g; print STDERR "renaming $File::Find::name to $newName\n"; # rename($File::Find::name, $newName) or die "can't rename : $!\n"; }, $homedir ); close DATABASE; close DATABASE2;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-04-18 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls