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 22:27
#186400 #186400
User since
2006-01-27
3875 Artikel
HausmeisterIn

user image
Hier ein Ansatz, der die genannten Bedingungen berücksichtigen sollte.
Hier läuft alles in einer Subroutine ab; Dateien einlesen, Datei-Erweiterung anpassen, mtime vergleichen und ggfs. Ausgabe generieren.


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
#! perl
use strict;
use warnings;

use 5.010;

use Data::Dumper;   # brings Dumper for Checking


# find_files( "/dirA" => "extA", "/dirB" => "extB" );
# extensions without dot
sub find_newer_files {
    my ( $dirA, $extA, $dirB, $extB ) = @_;

    # read files and their mtime from dirA into a hash (fname => mtime)
    chdir $dirA     or die "chdir($dirA) failed: $!";
    my %files_in_A = map { $_ => (stat $_)[9] } glob("*.$extA");

    # read files and their mtime from dirB into a hash (fname => mtime)
    chdir $dirB     or die "chdir($dirB) failed: $!";
    my %files_in_B = map { $_ => (stat $_)[9] } glob("*.$extB");

    # for each found file in dirA
    for my $fileA ( keys %files_in_A ) {
        # replace extension for wanted file in dirB
        ( my $fileB = $fileA ) =~ s{\.\Q$extA\E$}{.$extB};

        warn "$fileA -> $fileB\n"  if $ENV{DEBUG};

        # if we found a fileB in dirB
        if ( exists $files_in_B{$fileB} ) {
            # compare the timestamp; tell us when fileB is older than fileA
            if ( $files_in_A{$fileA} > $files_in_B{$fileB} ) {
                say "$fileB is older than $fileA";
            }
        }
    }

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

}


find_newer_files(
    "/tmp/A" => "adl",        # directory_A => extension_A
    "/tmp/B" => "adlt",
);
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?