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;
}
}
|