Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: opendir and directories that have spaces in them

by Anonymous Monk
on Aug 11, 2001 at 00:58 UTC ( [id://104048]=note: print w/replies, xml ) Need Help??


in reply to Re: opendir and directories that have spaces in them
in thread opendir and directories that have spaces in them

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 == "")

Replies are listed 'Best First'.
Re: Re: Re: opendir and directories that have spaces in them
by bikeNomad (Priest) on Aug 14, 2001 at 04:19 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://104048]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-03-19 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found