Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: how to rename all files in a dir

by flounder99 (Friar)
on Jul 07, 2002 at 04:18 UTC ( #179936=note: print w/replies, xml ) Need Help??

in reply to how to rename all files in a dir

File::Copy has a handy move function. The most important thing to remember is that readdir does not return the path just the files in the specified dir. So you have to remember tack the path back on before doing anything else like file tests or renaming.
use strict; use File::Copy qw ( move ); my $dirname = "./temp"; # dir you want to open; my $iterator = 0; opendir (DIRHANDLE, $dirname) or die "could not open dir: $!"; foreach (readdir(DIRHANDLE)) { #need to add path my $filename = $dirname . "/$_"; next unless -f $filename; move ($filename, $dirname . "/" . $iterator++) or warn "could not +rename file"; }



Replies are listed 'Best First'.
Re: Re: how to rename all files in a dir
by BrowserUk (Pope) on Jul 07, 2002 at 05:01 UTC

    Having been bitten by the "unadorned filename" return of readdir (isn't every newbie?), I got to wondering if there is ever the situation where you do not want the the full path? If so, how often, relative to the times when you do?

    I guess it's way to late to consider that this might be changed, given the golden rule that "Thou shalt not break old code".

    However, as it seems that Perl6 is going to comprehensively break this rule - maybe a new default would be in order?

      If so, how often, relative to the times when you do?

      While I share your pain (and frequently stumb my toe on this behavior), I think this idiom will help you. Use the chdir() function to get to the desired directory, then read away. That way, your can use the result of readdir directly.

      use CWD; my $org_dir = cwd; chdir($desired_dir) || die "can't cd: $!"; opendir(DIR, $desired_dir) || die "can't opendir: $!"; while(my $file = readdir DIR){ next if -d $file; # preform operations below on $file } closedir DIR; chdir($org_dir) || die "can't get back to $org_dir: $!";
      Personally I like it the way it is - it is much harder to remove the path from a fully specified pathname than to add the path to an unadorned one. It might be helpful to have it as an option to readdir, though.

      Makeshifts last the longest.

      Its not a matter of not wanting the full path... readdir() can't really give you the full path. It take a DIRHANDLE as an argument, and there is no fool-proof way of getting the correct "full path" from a DIRHANDLE.


      The real poison pill here is symlinks. Let's say you chdir to a location on your file system and read the listings there. Perl has no way of knowing if you came to that directory through 'hard' links or symlinks. And what is returned by pwd from the command line may or may not match up with what that command will return within perl.

      So, the answer to your question is, you probably DO want the full path in many situations, but there is no way to automatically get there from within a script. You will usually end up putting in some code that forces Perl to use a particular directory location.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2020-03-29 22:07 GMT
Find Nodes?
    Voting Booth?
    To "Disagree to disagree" means to:

    Results (171 votes). Check out past polls.