Thread Verzeichnis rekursiv auslesen und in Datenbank schreiben (39 answers)
Opened by Anton at 2012-05-15 08:38

topeg
 2012-05-23 17:20
#158523 #158523
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ohne es getestet zu haben

Variante über DBI:
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
#!/usr/bin/perl

use strict;
use warnings;
$| = 1;

use File::Find;
use DBI;

# Verzeichnis von Kommandozeile holen
my $verzeichnis = shift(@ARGV) || 'C:\Users\Ordner';
my $ausgabe_verzeichnis = shift(@ARGV) || 'C:\CSV_Ordner';
my $cvs_name = "dir.csv"

my $id = 0;
my $parent;
my %ids;

my $dbh = DBI->connect ("dbi:CSV:", undef, undef, {
          f_dir => $ausgabe_verzeichnis,
          csv_tables => {
            dir  => { file => $cvs_name }
          },
        });

$dbh->do ("DROP TABLE dir");
$dbh->do ("CREATE TABLE dir (fullname  CHAR (64), name CHAR (64),size INTEGER,id INTEGER,pid INTEGER,mtime INTEGER)");

my $sth=$dbh->prepare('INSERT INTO dir (fullname,name,size,id,pid,mtime) VALUES(?,?,?,?,?,?)');

sub wanted {
  return if ( $_ eq '..' );
  return if ( $_ eq '.' );
  $ids{ $File::Find::name } = $id;
  $parent = $File::Find::dir;
  $sth->execute(
         $File::Find::name,
         $_,
         (stat($File::Find::name))[7],
         $ids{ $parent },
         (stat($File::Find::name))[9],
       );
  }
}

$sth->finish();
$dbh->disconnect();


Variante mit Text::CSV:
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
#!/usr/bin/perl

use strict;
use warnings;
$| = 1;

use File::Find;
use Text::CSV;

# Verzeichnis von Kommandozeile holen
my $verzeichnis = shift(@ARGV) || 'C:\Users\Ordner';
my $csv_name = shift(@ARGV) || 'C:\CSV_Ordner\dir.csv';

my $id = 0;
my $parent;
my %ids;

my $csv=Text::CSV->new();
open(my $cvsfh,'>',$csv_name) or die("ERROR open $csv_name $!\n");
peint $cvsfh "#fullname,name,size,id,pid,mtime\n";

sub wanted {
  return if ( $_ eq '..' );
  return if ( $_ eq '.' );
  $ids{ $File::Find::name } = $id;
  $parent = $File::Find::dir;
  $csv->print ($cvsfh, [
         $File::Find::name,
         $_,
         (stat($File::Find::name))[7],
         $ids{ $parent },
         (stat($File::Find::name))[9],
       ]);
  }
}

View full thread Verzeichnis rekursiv auslesen und in Datenbank schreiben