in reply to Re: rename files
in thread rename files
open(my $fh, $namefile) or die "$namefile: $!"; while (<$fh>) { rename ($_, "testfile$_") or warn "rname $_: $!"; } close($fh);
This does not meet the spec given, even if you chomp the lines. Even if the data file was changed to contain the current names of the files, it wouldn't meet the spec given. The names of the files pbaumgar has and the names desired cannot be reconciled by concatenation alone.
RoyCrowder's solution achieves the desired effect if the data file all follows from the example. Otherwise, keeping the file of new filenames as in the spec would be more desirable, like this minor modification to RoyCrowder's existing code:
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; my @files = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR); closedir(DIR); @files = sort {$a <=> $b} @files; # Ascending sort open my $names, '<', 'newnames.txt' or die "Cannot read newnames.txt: +$!\n"; foreach (@files) { my $name = <$names>; chomp $name; rename($_, $name) or warn "rename: $_ : $!"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: rename files
by lostjimmy (Chaplain) on Apr 08, 2009 at 16:49 UTC | |
by mr_mischief (Monsignor) on Apr 08, 2009 at 17:03 UTC | |
by RoyCrowder (Monk) on Apr 08, 2009 at 18:01 UTC | |
by mr_mischief (Monsignor) on Apr 08, 2009 at 21:47 UTC | |
by pbaumgar (Acolyte) on Apr 08, 2009 at 23:49 UTC |