in reply to Re: Rename All Files In a Directory
in thread Rename All Files In a Directory
Here you hardcode a string and immedeatly chop off the last char. I cannot make any sense of that.$dir="/home/yehuda/img/jpg/"; chop $dir;
Here you do not check if the regex matches. If there is file that does not meet your assumptions it will horribly fail.while (defined ($img=readdir DH)){ $img=~m/^(.*)\.(.*)\.(.*)/g; $newname="$1.$3";
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.push @names,$img; push @newnames,$newname; }
close() closes a filehandle, not a dirhandle. use closedir();close DH;
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.foreach $name(@names){ $i=$i+1 ; $newname_=$newnames[$i-1];
Two unneccessary variables.$path="$dir/$name"; $target="$dir/$newname_";
Why chomp? there is no newline insight.chomp($target,$path);
I'd prefer a warning here instead of sudden death.rename ($path,$target) or die "Could not rename:$!"; print " $path is now $target \n";
$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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Rename All Files In a Directory
by blazar (Canon) on Jul 01, 2005 at 09:57 UTC | |
by holli (Abbot) on Jul 01, 2005 at 10:01 UTC |