Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello all,

I recently completed a small project here at work that involved renaming files based on existing file names. All of the files are named like this: a 10 digit base number, 1 letter 'x', an optional number, then the extension '.las'.
1234567890x.las 1234567890x1.las 1234567890x2.las
Many of the files share the same base number so that's why the optional number after the letter is needed.
There are around 4,000 files already on a network drive. New files that are to be added, need to have their prospective names compared to the existing files and the optional number added or incremented to make sure nothing will be overwritten. The files are then moved to a different folder to verify their header information before being moved to the final directory. Here's the code I used originally, and although the initial file globbing is slow, the actual renaming is quite fast.
#!/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; }
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 $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; } }
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
my @files2 = glob("$dir2/*.las");
than it does in the newer code
while (<$loc_dir*$from>) {
Can anyone explain this? Thanks in advance!

^l9v

In reply to Dynamic file renaming and globbing by licking9Volts

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found