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

O wise ones
I'm new to Perl and have run into a bit of a roadblock on a script I'm writing that renames files in a directory. I believe the problem involves the rename command and how it is used with an array in a for loop. the program prints out all the correct values, but it doesn't perform the renaming of the file. I've also tried using the shell command mv in place of rename, but the results are the same. any help would be greatly appreciated

### #Use the unix ls command to list the jpg files in the subdirectory ### use Shell(ls); @array = ls *jpg ; ### #Set the TotFiles scalar to the value number of elements in @array ### $TotFiles = @array + 1; print "Value of TotFiles is: $TotFiles \n"; ## #create four digit padding for the frames with a for loop ## for( $loop = 0; $loop <= $TotFiles; $loop++ ) { if ($loop < 10) { $padder = '000'; } if (($loop > 10) && ($loop < '100')){ $padder = '00'; } if (($loop > 100) && ($loop < 1000)) { $padder = '0'; } if ($loop > 1000) { $padder = ''; } ## #create a new name using the padder and the loop number ## $new = "snowscene"."."."$padder"."$loop".".jpg"; print "$new \n"; $old = @array[[$loop]] ; print $old; rename ($old, $new) ; }

holli fixed formatting

Replies are listed 'Best First'.
Re: renaming a list of files
by Zaxo (Archbishop) on Aug 17, 2005 at 06:44 UTC

    You should look at sprintf. With it you can map numbers into numeric strings with leading zeros.

    for (0..102) { $_ = sprintf "%04d\n", $_; print; }
    You can use that in your loop as, my $new = 'snowscene.' . sprintf( '%04d', $loop) . '.jpg';

    After Compline,
    Zaxo

Re: renaming a list of files
by graff (Chancellor) on Aug 17, 2005 at 08:09 UTC
    it doesn't perform the renaming of the file. I've also tried using the shell command mv in place of rename, but the results are the same

    Have you checked whether you have write permission on the directory that contains the files? On a unix-type system, if you own the directory, you can fix the permission problem yourself with "chmod" (command line or perl function); if you don't own it, you'll need to get the directory owner (or a superuser) to grant write permission on the directory (or, create another directory yourself somewhere, and put copies of the files there with the names that you want).

Re: renaming a list of files
by japhy (Canon) on Aug 17, 2005 at 15:21 UTC
    You've got a LOT of broken code in there. First of all, your 'ls' isn't working. Try: @array = ls '*.jpg' instead. Next, to get the total number of files, just use @array, not @array + 1. And then your for loop should be going one less iteration than you've written ($loop < @array). Your assignment to $new gives me a headache; use: $new = "snowscene.$padder$loop.jpg". Finally, your assignment to $old is broken; use: $old = $array[$loop].

    You need to step back and look at some introductory Perl material; I have a feeling you're learning Perl from the wrong sources.


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: renaming a list of files
by QM (Parson) on Aug 17, 2005 at 18:23 UTC
    At the risk of swamping a neophyte with too much information, here is what I hacked up for myself.

    It's not perfect, but has the following features: doesn't clobber existing files (unless you ask it), collisions can create unique file names, will show new names without renaming, and uses perl code (typically regex) for the mapping.

    Here's the code:

    If I had it to do over, I'd probably use Getopt::Declare; drop in my own hack of DOSGLOB (which takes named parameters, and will warn on empty matches); and handle DOS pathnames better.

    I basically stole this code from somewhere else (perhaps a FAQ? Cookbook?), and made it work for me.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of