eilat has asked for the wisdom of the Perl Monks concerning the following question:

I have a UNC path with several zip files under it. I would like to get the most updated zip file under this UNC directory, and then copy this zip file to a local folder Please assist

  • Comment on Get the last modified file from the UNC path

Replies are listed 'Best First'.
Re: Get the last modified file from the UNC path
by Corion (Patriarch) on Aug 27, 2023 at 11:38 UTC

    The easiest way is to use readdir together with stat and sort.

    Something like this:

    use 5.020; my $dir = '\\\\server\\share\\my\\path'; opendir my $dh, $dir or die "Can't opendir '$dir': $!"; my @files = map { "$dir\\$_" } # convert filenames to absolute filen +ames with UNC path grep { !/^\.\.?\z/ } # ignore "." and ".." readdir( $dh ); my %last_modified = map { $_ => -M $_ } # cache the last modified time @files; @files = sort { $last_modified{ $a } <=> $last_modified{ $b } } @files +; say $files[0];

    Update: Ignore . and .. entries, spotted by Marshall

Re: Get the last modified file from the UNC path
by tybalt89 (Monsignor) on Aug 27, 2023 at 17:12 UTC

    TIMTOWTDI

    #!/usr/bin/perl use strict; # https://www.perlmonks.org/?node_id=11154077 use warnings; use List::AllUtils qw( min_by ); my $dir = '.'; # FIXME to your path my $mostrecent = min_by { -M } <$dir/*>; print "most recent file: $mostrecent\n";