I used Perl::Tidy to reformat your script, making it easier to read.

#!/usr/bin/perl use warnings; use strict; use File::Copy; my $srcdir = "\\\\windowsservername\\directory\\Subdirectory\\"; my $dest = "C:\\Documents and Settings\\"; for ( ; ; ) { opendir( DIR, $srcdir ) or die "Can't open $srcdir: $!"; my @files = grep { !/^\.+$/ } readdir(DIR); @sorted = reverse( sort { -M $a <=> -M $b #( $m{$a} ||= -M $a ) <=> # ( $m{$b} ||= -M $b ) } @files ); close(DIR); if ( !@sorted ) { print "Files have been ended.\n\n"; last; } $xmlfile = $sorted[0]; 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 }

You should be using closedir to close the directory handle, rather than close. But I don't think this is causing your problem.

As Marshall has pointed out, your tests for file modification time are not using the correct paths. This is the probable cause of the "Use of uninitialized value in numeric comparison (<=>) at c:\1file@atime.pl line 17." errors you are getting. Prepend the directory to the file names in the tests and you will probably find that your list is sorted.

You could use glob to get the filenames, rather than reading the directory. Glob can return paths, rather than just file names. Something like the following is an alternative:

my $oldestfile; my $oldestage; foreach my $file (glob("$srcdir/*.xml")) { if( !defined($oldestfile) or -M $file > $oldestage ) { $oldestfile = $file; $oldestage = -M $file; } }

In reply to Re: Sort files by oldest timestamp by ig
in thread Sort files by oldest timestamp by srianju

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.