#! perl use strict; use warnings; use 5.010; use Data::Dumper; # brings Dumper for Checking # Check files in these directories my @dirs = ( 'C:/temp/A', 'C:/temp/B', ); # read files from given directory; store mtime in Hash-of-Hash sub get_mtime_of_files_in_dir { my %arg = @_; die "no dir argument specified\n" unless $arg{dir}; die "no storage hashref specified\n" unless $arg{store}; die "store is not a hashref\n" unless ref($arg{store}) eq 'HASH'; chdir $arg{dir} or die "chdir($arg{dir} failed: $!"; opendir my $dh, $arg{dir} or die "opendir($arg{dir}) failed: $!"; my @files = grep { ! -d } readdir($dh); closedir $dh; for my $file ( @files ) { $arg{store}->{$file}->{$arg{dir}} = ( stat $file )[9]; } } # store the information about files, directories and mtimes in this hash # %hash = ( # filename => { # dirname1 => mtime(filename), # dirname2 => mtime(filename), # }, # ... # ); my %hash; # find files in configured dirs and store results in %hash for my $dir ( @dirs ) { get_mtime_of_files_in_dir( dir => $dir, store => \%hash, ); } if ( $ENV{DEBUG} ) { warn Data::Dumper->new( [ \%hash ], [ '*hash'] )->Dump, "\n\n"; } # show where are the newest versions of each file for my $file ( keys %hash ) { # get dirs where file was found my @dirs = keys %{ $hash{$file} }; # if found in only 1 directory if ( 1 == @dirs ) { say "$file is only in $dirs[0]"; } # if found in more than one else { # find the newest file version my $newest = ( sort { $hash{$file}->{$b} <=> $hash{$file}->{$a} } @dirs )[0]; # tell user where the newest file is say "$file is newest in $newest"; } }