licking9Volts has asked for the wisdom of the Perl Monks concerning the following question:
Many of the files share the same base number so that's why the optional number after the letter is needed.1234567890x.las 1234567890x1.las 1234567890x2.las
Just this morning, I found this post on Renaming Files by tachyon and it looked like it would work more efficiently than mine. I modified it to fit my situation and found that it DID work better than my original. Here's the new code I used:#!/usr/bin/perl -w use strict; my $dir1 = "h:/andrew/bruner/temp/stage/copies"; my $dir2 = "j:/templas"; my $field1 = " "; my $field2 = " "; my $testflag = '0'; my $newname = " "; my $oldname = " "; my $counter = '0'; print "\nGlobbing 1st set..."; my @files1 = glob("$dir1/*.las"); print "done\nGlobbing 2nd set..."; my @files2 = glob("$dir2/*.las"); print "done.\n"; foreach $field1 (@files1) { $testflag = '1'; $oldname = $field1; while ($testflag == '1') { foreach $field2 (@files2) { if (substr($field1, 35, 11) eq substr($field2, + 11, 11)) { $counter++; $field1 = substr($field1, 0, 46) . $co +unter . ".las"; print "Trying $field1\n"; } else { $testflag = '0'; } } } if ($counter >= '1') { $newname = substr($field1, 0, 46) . $counter . ".las"; } else { $newname = substr($field1, 0, 46) . ".las"; } push (@files2, $newname); rename $oldname, $newname; print "$oldname renamed to $newname"; $counter = 0; }
This works exactly like I need it to. So many thanks to you, tachyon, for sharing your code. One thing that puzzles me though is why it takes so much longer for the file globbing in the original code#!/usr/bin/perl -w use strict; my $loc_dir = 'h:/andrew/bruner/temp/stage/'; my $to_dir = 'h:/andrew/bruner/temp/stage/copies/'; my $net_dir = 'j:/templas/'; my $from = '.las'; my $counter = '0'; my ($old, $new, $name); my @usedarray; while (<$loc_dir*$from>) { $old = $new = $_; $new =~ s/^$loc_dir/$net_dir/; while (exist($new)) { $counter++; $new = substr($new, 0, 22) . $counter . ".las"; } $counter = '0'; $new =~ s/^$net_dir/$to_dir/; rename $old, $new; print "\nRenamed $old $new"; } sub exist { my $file = shift; if (-e $file) { print "\nFile $file exists."; return 1; } $file =~ s/^$net_dir/$to_dir/; if (-e $file) { print "\nFile $file exists."; return 1; } }
than it does in the newer codemy @files2 = glob("$dir2/*.las");
Can anyone explain this? Thanks in advance!while (<$loc_dir*$from>) {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamic file renaming and globbing
by jdporter (Paladin) on Jan 13, 2003 at 21:15 UTC | |
|
Re: Dynamic file renaming and globbing
by BrowserUk (Patriarch) on Jan 14, 2003 at 06:01 UTC | |
|
Re: Dynamic file renaming and globbing
by waswas-fng (Curate) on Jan 14, 2003 at 03:26 UTC |