in reply to Re: Rename All Files In a Directory
in thread Rename All Files In a Directory

A little walkthrough
$dir="/home/yehuda/img/jpg/"; chop $dir;
Here you hardcode a string and immedeatly chop off the last char. I cannot make any sense of that.
while (defined ($img=readdir DH)){ $img=~m/^(.*)\.(.*)\.(.*)/g; $newname="$1.$3";
Here you do not check if the regex matches. If there is file that does not meet your assumptions it will horribly fail.
push @names,$img; push @newnames,$newname; }
I'm not sure why you don't rename the files immedeatly, but ok. i can live with that. Even if you really have to store the filenames somewhere i would use a hash for that. old name as key, new name as value.
close DH;
close() closes a filehandle, not a dirhandle. use closedir();
foreach $name(@names){ $i=$i+1 ; $newname_=$newnames[$i-1];
Now that is the fun part. You add 1 to $i just to substract the 1 again when you use $i. Better to do the addition after the usage and leave out the substraction.
$path="$dir/$name"; $target="$dir/$newname_";
Two unneccessary variables.
chomp($target,$path);
Why chomp? there is no newline insight.
rename ($path,$target) or die "Could not rename:$!"; print " $path is now $target \n";
I'd prefer a warning here instead of sudden death.

After all I doubt that you got this code from the cookbook. And if so, throw it into the trashcan. Slightly improved:
$dir="/home/yehuda/img/jpg"; opendir(DH,$dir)or die "FOO BAR! $!\n"; while ( readdir DH) { if ( -f "$dir/$_" && $m/^(.*)\.(.*)\.(.*)/ ) { warn "cannot rename $dir/$_\n" unless rename "$dir/$_", "$dir/$1.$3"; } } closedir DH;


holli, /regexed monk/

Replies are listed 'Best First'.
Re^3: Rename All Files In a Directory
by blazar (Canon) on Jul 01, 2005 at 09:57 UTC
    $dir="/home/yehuda/img/jpg/"; chop $dir;
    Here you hardcode a string and immedeatly chop off the last char. I cannot make any sense of that.
    It does have some minimal amount of sense, if seen in the awkward context of all the rest of the code. Probably the trailing / conveys him the psychological feeling of having to do with a directory, but then later he does $dir/$somethingelse. Not to justify him - just trying to understand...

    Incidetally, why did you post your reply to the OP as a reply to my own reply?!?

      Because in your OP, before you updated it, there was something along the lines of "your code does not look so bad".


      holli, /regexed monk/