Thread Wie kann ich das Datum verschiedener Files vergleichen? (13 answers)
Opened by guest mo at 2017-04-27 16:01

Linuxer
 2017-04-27 17:08
#186394 #186394
User since
2006-01-27
3875 Artikel
HausmeisterIn

user image
Du hast doch im Prinzip schon alles; wo genau liegt nun Dein Problem?

Perldoc:perlfunc stat liefert eine Liste von Werten; die Du ja auch schon in Arrays speicherst.
Jetzt musst Du nur noch für Dich entscheiden, welchen dieser Werte Du vergleichen willst (oft ist das "mtime"); dann vergleichst Du diese Werte und auf Basis des Vergleichsergebnisses tust Du das, was Du dann eben tun willst.

;-)

Update:
Ich hab mal ein wenig rumgespielt; hier eine Variante, wie man sowas machen könnte. Weitere Logik muss noch eingebaut werden (z.B. was gilt, wenn Dateien gleich alt sind?).

more (16.2kb):

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! perl
use strict;
use warnings;

use 5.010;

use Data::Dumper;   # brings Dumper for Checking

# Check files in these directories
my @dirs = (
    'C:/temp/A',
    'C:/temp/B',
);



# read files from given directory; store mtime in Hash-of-Hash
sub get_mtime_of_files_in_dir {
    my %arg = @_;
    
    die "no dir argument specified\n"       unless $arg{dir};
    die "no storage hashref specified\n"    unless $arg{store};
    die "store is not a hashref\n"          unless ref($arg{store}) eq 'HASH';

    chdir $arg{dir}             or die "chdir($arg{dir} failed: $!";

    opendir my $dh, $arg{dir}   or die "opendir($arg{dir}) failed: $!";
    my @files = grep { ! -d } readdir($dh);
    closedir $dh;

    for my $file ( @files ) {
        $arg{store}->{$file}->{$arg{dir}} = ( stat $file )[9];
    }

}




# store the information about files, directories and mtimes in this hash
# %hash = (
#    filename => {
#         dirname1 => mtime(filename),
#         dirname2 => mtime(filename),
#    },
#    ...
# );
my %hash;


# find files in configured dirs and store results in %hash
for my $dir ( @dirs ) {
    get_mtime_of_files_in_dir( dir => $dir, store => \%hash, );
}

if ( $ENV{DEBUG} ) {
    warn Data::Dumper->new( [ \%hash ], [ '*hash'] )->Dump, "\n\n";
}

# show where are the newest versions of each file
for my $file ( keys %hash ) {

    # get dirs where file was found
    my @dirs = keys %{ $hash{$file} };

    # if found in only 1 directory
    if ( 1 == @dirs ) {
        say "$file is only in $dirs[0]";
    }
    # if found in more than one
    else {
        # find the newest file version
        my $newest = (
            sort { $hash{$file}->{$b} <=> $hash{$file}->{$a} } @dirs
        )[0];

        # tell user where the newest file is
        say "$file is newest in $newest";
    }

}


Testlauf:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\TEMP>perl comp_files.pl
comp_files.pl is newest in C:/temp/A

C:\TEMP>set DEBUG=1

C:\TEMP>perl comp_files.pl
%hash = (
'comp_files.pl' => {
'C:/temp/B' => 1493306489,
'C:/temp/A' => 1493307421
}
);


comp_files.pl is newest in C:/temp/A




edit: Überflüssiges Wort entfernt.

Last edited: 2017-04-27 21:47:51 +0200 (CEST)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Wie kann ich das Datum verschiedener Files vergleichen?