在两个单独的目录中查找两个最新文件并合并它们

时间:2012-07-25 13:23:15

标签: perl

我有两个可能的图片。区域目录每5分钟更新一次,监视目录每15更新一次。

我要做的是找到每个目录中的最新文件并获取这些文件并使用 Image Magik 创建第三张图片。

我对某些人有用,但非常不一致,例如,当时间与手表文件匹配时,我的代码有时会遗漏区域文件。

其他时候它会合并两个监视文件,即使监视文件和区域文件位于两个单独的目录中。

我不知道如何解决它。
这是我的代码:

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;

use vars qw/%files_watch/;
use vars qw/%files_regional/;

sub findNewestFiles {
    my $element = $File::Find::name;
    return if ( !-f $element );

    $files_watch{$element} = ( stat($element) )[10];
    $files_regional{$element} = ( stat($element) )[10];

}

my $image_magick_exe = "composite.exe\"";
my $pic_dir = "C:\\eterra\\eterravision\\weather";
my $watch_dir = "C:\\eterra\\eterravision\\weather\\watch";
my $regional_dir = "C:\\eterra\\eterravision\\weather\\regional";

open( OUT, ">>names.txt" ) || die;

find( \&findNewestFiles, $watch_dir );
my $newestfile_watch;
my $time_watch = 0;

while ( my ( $t1, $t2 ) = each(%files_watch) ) {
    if ( $t2 > $time_watch ) {
        $newestfile_watch = $t1;
        $time_watch       = $t2;

    }

}

$time_watch = localtime($time_watch);

find( \&findNewestFiles, $regional_dir );
my $newestfile_regional;
my $time_regional = 0;

while ( my ( $t3, $t4 ) = each(%files_regional) ) {
    if ( $t4 > $time_regional ) {
        $newestfile_regional = $t3;
        $time_regional       = $t4;

    }
}

$time_regional = localtime($time_regional);

$newestfile_watch =~ s/\//\\/g;
$newestfile_regional =~ s/\//\\/g;    #replacing the "/" in the file path to "\"

my @temp = split( /_/, $newestfile_regional );

my $type = $temp[0];
my $date = $temp[1];
my $time = $temp[2];

my $check = "$pic_dir/radarwatch\_$date\_$time";    #check if file was created

unless ( -e $check )

{

    system("\"$image_magick_exe \"$newestfile_regional\" \"$newestfile_watch\"      \"$pic_dir\\radarwatch\_$date\_$time\"");

    print "file created\n";
}

我将子功能中的[10]更改为[8]和[9]。 8是访问时间,9是修改时间,10是创建时间,10是最成功的。

我认为问题在于子功能。 有没有更好的方法来搜索最新的创作时间?比我的更可靠的东西?

1 个答案:

答案 0 :(得分:0)

我认为你的问题的关键是找到每个目录中的最新文件,然后处理它们。暂且不论处理它们的细节,这里有一个查找最新文件的脚本。我省略了imagemagick的东西,这些东西都可以放入process_latest子程序中。不需要File :: Find。 File :: stat允许我们使用名称而不是试图记住这些数字。该计划有一个更清晰的结构。

use strict;
use warnings;
use File::stat;

my $watch_dir    = "C:\\eterra\\eterravision\\weather\\watch";
my $regional_dir = "C:\\eterra\\eterravision\\weather\\regional";

# get the latest in each directory
my $latest_regional = get_latest_file($regional_dir);
my $latest_watch    = get_latest_file($watch_dir);

# do whatever you want here...
process_latest ($latest_regional, $latest_watch);

# I exit 1 in Windows, exit 0 in Unix
exit 1;

#--------------------------------
# subroutines
#--------------------------------

sub get_latest_file {
    my $dir = shift;
    opendir my $DIR, $dir or die "$dir $!";
    my $latest_time = -1;
    my $latest_file = '';
    FILE:
    while (readdir($DIR)) {
        my $file = "$dir\\$_";
        next FILE unless -f $file;
        my $file_time = stat($file)->mtime;
        print "$file $file_time\n";
        if ($file_time > $latest_time) {
            $latest_time = $file_time;
            $latest_file = $file;
        }
    }
    closedir $DIR;
    return $latest_file;
}

sub process_latest {
    my $regional = shift;
    my $watch    = shift;

    print "Latest Regional: $regional\n";   
    print "Latest Watch:    $watch\n";

}