I threw together a solution that is very similar to what
Kenosis has suggested. This code creates a
%file_info hash. In the code below, the last file name encountered for a given prefix gets stored in the hash. If you want to do other things (like keep track of file type, etc.) then you could modify the
%file_info hash. Basically, you want to create a data structure that contains all the information you want to track.
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
use File::Find::Rule;
my $dir = shift or die 'No directory given.';
my $dir_path = Path::Tiny->new($dir);
my @files = File::Find::Rule->file()->in( $dir_path );
my %file_info;
foreach my $file (@files) {
my $file_name = path($file)->basename;
my $fn_without_ext = $file_name;
$fn_without_ext =~ s/\..{3}$//;
$file_info{ path($file)->dirname }{ $fn_without_ext } = path($file
+)->basename;
}
foreach my $found_dir (keys(%file_info)) {
my $file_name = (values %{$file_info{$found_dir}})[0];
my $file_to_process = path($found_dir, $file_name);
#process the file here (e.g. use the 'system' command)
print "Processing this file: $file_to_process\n";
}
exit;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.