Hi all,

I'd like to know if it is possible to create a perl script that would take the names of image files with its extension of say 10,000 images from out of a folder and place them into a database table in separate rows to use as sample data?

Thank you

Recommended Answers

All 2 Replies

The following script creates a table (if it doesn't already exist) and saves all the image names (extension included) and the directory names.

#!/usr/bin/perl
use strict; 
use warnings; 
use DBI;
use File::Glob ':glob';
use File::Basename;

my $dbh=DBI->connect('dbi:mysql:daniweb','david','dogfood') ||
   die "Error opening database: $DBI::errstr\n";

$dbh->do("CREATE TABLE IF NOT EXISTS images (id INT NOT NULL AUTO_INCREMENT,
                                i_name VARCHAR(64),
                                i_dir VARCHAR(100),
                                PRIMARY KEY (id))");

my $sth = $dbh->prepare("insert into `images` (i_name, i_dir) values (?, ?)")
             or die "prepare failed: " . $dbh->errstr();  


my @files = </home/david/Pictures/*>;
foreach (@files) {
    my($filename, $dir) = fileparse($_);
    $sth->execute($filename, $dir) or die "execute failed: " . $sth->errstr(); 
}

# Close connection
$sth->finish();
undef($dbh);
commented: Nice one +3

Thanks for the help mate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.