Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

Hi is there a way I could move and rename nultiple files at the same time? For example: I have DIRECTORY1 that has files named: file1, file2, file3. I need to move them to DIRECTORY2 then append a string "processed" such that the moved files will now be named:file1_processed, file2_processed and so on.

Replies are listed 'Best First'.
Re: Move and rename files
by jwkrahn (Abbot) on Jun 29, 2006 at 03:09 UTC
    my $dir_1 = '/some/dir'; my $dir_2 = '/other/dir'; opendir DIR, $dir_1 or die "Cannot open '$dir_1' $!"; while ( my $file = readdir DIR ) { rename "$dir_1/$file", "$dir_2/${file}_processed" or die "Cannot m +ove '$dir_1/$file' $!"; }
      so there's no way to do it withouta loop? I was thinking mv command could do the job...
        What's wrong with a loop? Even a mv command would have to do some looping internally.

        You could of course write a mv subroutine, hide it away in a module, then use the module and call the mv sub/method. The users would not see the loop, but internally it will still be doing just that.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        Perl doesn't have a mv command.
Re: Move and rename files
by perl_lover (Chaplain) on Jun 29, 2006 at 08:00 UTC