Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Sort files by oldest timestamp

by codeacrobat (Chaplain)
on May 06, 2009 at 05:54 UTC ( #762169=note: print w/replies, xml ) Need Help??


in reply to Sort files by oldest timestamp

try
sort {(-M $a) <=> (-M $b)}
Update
Imho it is better to use stat to test the files. -M uses the $^T special variable, which is set once at the start of the script. For long running scripts this is not recommended. Try this (untested):
#!/usr/bin/perl use warnings; use strict; use File::Copy; my $srcdir = "\\\\windowsservername\\directory\\Subdirectory\\"; my $dest = "C:\\Documents and Settings\\"; LOOP: while (1) { undef $!; my %fileage = map { ($_ => (stat)[9]) } glob "$srcdir\\*.*"; die "can't glob $srcdir $!" if $!; my $xmlfile = (sort { $fileage{$a} <=> $fileage{$b} } keys %fileag +e)[0]; unless (defined $xmlfile){ print "Files have been ended.\n\n"; last LOOP; } my $old = "$srcdir/$xmlfile"; move( $old, $dest ) or die "Move $old -> $dest failed: $!"; print "File Name: $xmlfile moved to Fileshare - 30 mins for next uploa +d.\n\n"; sleep 1800; # 30 Minutes }

print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

Replies are listed 'Best First'.
Re^2: Sort files by oldest timestamp
by pelagic (Priest) on Oct 29, 2009 at 15:31 UTC
    ok, ok, I know, it's way old but I can't resist to say this:
    Your code does the LOOP as many times as there are files in the srcdir and it does a stat for ever file in srcdir in every LOOP as long as the file has not been moved yet (i.e. n! with n files). So if you have very many files in your srcdir, let's say 1,000,000 or so you will have a lot of unnecessary overhead.

    pelagic

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://762169]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2021-09-23 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?