Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

how to rename all files in a dir

by Anonymous Monk
on Jul 07, 2002 at 03:18 UTC ( [id://179927] : perlquestion . print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to loop thru a directory of files and change each file's name to the iterator's number. so, in some kind of pseudocode:

while not ren the last file in the dir get the next file's file name "and" rename the file end of while

A snippet of code would help - be a blessing. Thanks

Replies are listed 'Best First'.
(jeffa) Re: how to rename all files in a dir
by jeffa (Bishop) on Jul 07, 2002 at 03:25 UTC
    These things i tend to do with quick one-liners. Say you want to prepend the number to all .txt files:
    perl -e 'while(<*.txt>){$n=sprintf("%02d%s",++$i,$_);rename$_,$n}'


    (the triplet paradiddle with high-hat)
      In case there is no specified file extension like .txt, something like this can be done:

      perl -e 'for (grep {-f} <*>) { rename $_, sprintf("%02d%s", ++$i, $_) }'

      Zenon Zabinski | zdog |

Re: how to rename all files in a dir
by flounder99 (Friar) on Jul 07, 2002 at 04:18 UTC
    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"; }



      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.


Re: how to rename all files in a dir
by Juerd (Abbot) on Jul 07, 2002 at 10:15 UTC

    I want to... A snippet of code would help

    Hire a programmer or learn. Although many seem to think so, PerlMonks is no "Free custom scripts" site.

    If you want help, state a question, and let us know what you have already tried yourself. Make sure you have RTFM and used strict and warnings. Don't ask for code, ask for help. You'll probably get code anyway.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.