Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: File::Find problems

by bobf (Monsignor)
on Sep 10, 2004 at 20:03 UTC ( #390176=note: print w/replies, xml ) Need Help??


in reply to File::Find problems

I see ysth beat me to this one, but here is the code I was working on anyway. It uses the opendir/readdir/loop method:

use strict; use warnings; use File::Spec; # for portable filename operations use File::Path; # to create the new subdirectory use File::Copy; # to move the edited file my $currentdir = '.'; my $newdir = File::Spec->catdir( $currentdir, 'newdir' ); # make the new subdirectory mkpath( $newdir ) or die "error creating $newdir"; # get all filenames in the current directory opendir( DIR, $currentdir ) or die "couldn't open $currentdir\n"; my @filenames = readdir DIR; closedir DIR; # get the program name so it's not moved if $currentdir eq '.' my ( undef, undef, $progname ) = File::Spec->splitpath( $0 ); foreach my $file (@filenames) { # add the full path to the filename my $pathfile = File::Spec->catfile( $currentdir, $file ); # skip if $file is a directory or this program if( -d $pathfile or $file eq $progname ) { next; } # process file here # move file to new subdir my $newpath = File::Spec->catfile( $newdir, $file ); move( $pathfile, $newpath ) or print "error moving $file"; }

I'm sure more experienced Monks can come up with more elegant solutions, but this works on the tests I performed.

HTH

Update: Fixed code per parv's suggestion.

Replies are listed 'Best First'.
Re^2: File::Find problems
by parv (Vicar) on Sep 10, 2004 at 23:38 UTC
    my @filenames = readdir DIR; ... foreach my $file (@filenames) { if( -d $file or $file eq $0 ) ... }

    I doubt that the 2d part of the test will produce the desired result beacuse of ...

    readdir DIRHANDLE ... If you're planning to filetest the return values out of a "readdir", you'd better prepend the directory in question. Otherwise, because we didn't "chdir" there, it would have been testing the wrong file.
      Very good point. I set $currentdir to '.' to simplify the example, and forgot about it when I got to the loop. I'll update the code to include the full path of each $file. Thanks for pointing it out.
Re^2: File::Find problems
by hokie (Monk) on Sep 13, 2004 at 13:50 UTC
    Thanks all for the help and pointing me in the right dir(direction...not directory though in this case it is the same thing).
    SB

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-08-09 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?