LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-16-2003, 02:40 PM   #1
meluser
Member
 
Registered: Mar 2003
Posts: 65

Rep: Reputation: 15
Combining 2 perl scripts together


Hi

i have 2 perl scripts that do the following:

The first is named renamer.pl. the script looks in to a directory namely /home/httpd/htdocs for files named images.jpg(coming from camera). the script renames the images with the date of the image, and moves to a new location namly /home/me/images/. this script is running all the time.

The Second script takes image information (name, date, and size) from the images directory (../me/images/) and insets into MySQL.

What the problem i am having is that the second script only starts manually. is there away i could start it automatically?

I would however prefer something else instead of starting it automatically. I would prefer to combine both scripts, so that when the images are renamed and relocated, in the same time, the data is inserted into MySQL.

I would really appreciate any help. please find enclosed both scripts:

Regards,

Mel

-------------------------------------------renamer.pl
#!/usr/bin/perl
use strict;
use warnings;

=head1 NAME

# renamer - renames files received by ftp, moving them to a new directory

=head1 SYNOPSIS

nohup ./renamer image /home/httpd/htdocs /home/me/images jpg renamer.process &

=head1 DESCRIPTION

#The above instructs renamer to look for files called image.jpg in /home/httpd/htdocs.
#It checks once per minute for such a file to appear. If it sees a
#readable file called /home/httpd/htdocs.jpg it moves it to
#/home/httpd/htdocs/image.200302251530.jpg where the number is a
#time stamp with year (four digits), month, day of the month, hour (in
#24 mode), and minute.

#Read the bugs section closely.

=head1 BUGS

#The original and new directories must be on the same file system.
#The program probably does not work on windows systems.
#The daemon behavior is weak.
#Not much testing has been done, so the script may have other problems.

=cut

my $usage = <<EOUSAGE;
usage: $0 initial_name original_dir new_dir suffix lockfile
example: $0 pic /home/httpd/htdocs /home/me/images jpg /home/me/renamer.process
EOUSAGE

my $check_file = shift or die $usage;
my $original_dir = shift or die $usage;
my $new_dir = shift or die $usage;
my $suffix = shift or die $usage;
my $lockfile = shift or die $usage;



##################################
# If you put it into the cron, comment out between the START and END BLOCK, and uncomment
# the section below it so you don't get multiple copies running. Also, comment out the
# lockfile bits above.
#START BLOCK
exit if (fork());

while (-e "$lockfile") {
process($check_file) if (-r "$original_dir/$check_file.$suffix");
sleep 30;
}
#END BLOCK
##################################
#
# process($check_file) if (-r "$original_dir/$check_file.$suffix");
#
##################################
sub process {
my $file = shift;
my @st = (stat("$original_dir/$file.$suffix"));
my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime($st[10]);
$Year += 1900;
$Month++;
my $stamp = sprintf "%4d_%02d_%02d_%02d_%02d_%02d", $Year, $Month, $Day, $Hour, $Minute, $Second;
print "renaming $original_dir/$file.$suffix to $new_dir/$stamp.$suffix\n";
rename "$original_dir/$file.$suffix", "$new_dir/$stamp.$suffix" or warn "couldn't rename file: $! $file to $new_dir/$file.$stamp.$suffix\n";

############################
# Maybe i could add the MySQL insert here????
############################

}
______________________________________________________


------------------------------------------infoinsert.pl

#!/usr/bin/perl -w

use strict;
use DBI;
use Date::Manip;

############################################################################
#
# Connect to Database Named cctvimages on the localhost with the root user
# $dbh=DBI->connect(DBI:mysql;$database", $user, $password);
############################################################################
#

my $dbh = DBI->connect("DBI:mysql:dbname=cctvimages;host=localhost","root",
"********", {'RaiseError' => 1});

my $file;
my $size;
my $mtime;
my $secs;



my $imagedir="/home/me/images";
chdir($imagedir) or die "Can't cd to $imagedir; $!\n";
my @jpegs = <*.jpg>;
foreach (@jpegs) {
my $file = $_;





#$file = "/home/me/images/2003_03_11_14_32_42.jpg";

($size, $secs) = (stat ($file))[8,9];

my $mtime = &ParseDateString("epoch $secs");
# even after conversion ':' is used to seperate hh and mn and ss

$mtime =~ s/://g; # the above swaps out the ':' for nothing

$file =~ s/\/home\/me\/images\///; # the above strips path

print"size is $size\nmodified is $mtime\nfilename is $file\n";

my $rows_affected = $dbh->do("
INSERT INTO imageinfo
VALUES(null, '$file', '$size', '$mtime')
") or die "Do Fails: $DBI::errstr\n";

my $sql = "SELECT * FROM imageinfo";
my $sth = $dbh->prepare($sql);

my $sth = $dbh->prepare($sql);
$sth->execute or die"Execute fails: $DBI::errstr\n";
$sth->finish;
}


$dbh->disconnect;
 
Old 03-16-2003, 08:21 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Why not just have the first script execute the second??

I believe the perl command is something like:
system("/path/to/script/dir/myspq.pl");

Or you could take the entire second script and copy-&-paste it into s ubroutine definition in the first script. Then call it as appropriate.
 
Old 03-16-2003, 08:23 PM   #3
meluser
Member
 
Registered: Mar 2003
Posts: 65

Original Poster
Rep: Reputation: 15
thanks for the help.

but then it will still take all data from /images.

Could you please help me.
 
Old 03-16-2003, 09:05 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
All you have to do is modfy your SQL script to go where you have the comments in your first script. Instead of having it do every file in the dir, take the filename you used when copying the file over. Get rid of your foreach loop and isntead of using $_ in your initial assignment to $file, use "$new_dir/$stamp.$suffix" from the first script.
 
Old 03-16-2003, 09:55 PM   #5
meluser
Member
 
Registered: Mar 2003
Posts: 65

Original Poster
Rep: Reputation: 15
thanks for the help.

i have tried to put it together, but i think i am doing it wrong. could you please show me how to do it. i do want to make errors.

thanks very much.
 
Old 03-18-2003, 06:46 AM   #6
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,198

Rep: Reputation: 76
why dont u enclose the second file as a sub in the first file?

sub insert {
do what u like..
}

and then call it from the main program

&insert;
u may call it with the values u want to insert as arguments
&insert($value1, $value2, $value3);
these values will be in @_ then
so u can retrieve them in the sub as
$_[0];
$_[1]; and so on

ok?
cheers, jens
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell and perl scripts srnerkar1 Programming 2 12-03-2005 03:07 PM
bash & perl scripts scribbler001 Programming 1 04-26-2005 04:56 AM
my perl scripts wont run need help unixmad Linux - Software 4 09-14-2004 08:04 PM
Irssi and perl and scripts? MrDoctor Linux - Newbie 0 04-05-2004 09:44 AM
perl scripts mimf Linux - Newbie 3 12-01-2003 11:10 PM


All times are GMT -5. The time now is 08:41 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration