LinuxQuestions.org
Help answer threads with 0 replies.
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
  Search this Thread
Old 03-24-2003, 01:23 PM   #1
meluser
Member
 
Registered: Mar 2003
Posts: 65

Rep: Reputation: 15
Loop in this perl script is not working


Hi

I have this script that is not working as it should be. this is what it is supposed to be doing, take images from a directory (images are coming from camera) and renames them with the date and time of image. then insert some meta information into MySQL. what the script is supposed to be doing is to always keep looking and whenever it finds images to perform the change and insert. the output is as follow: it only does the process once.

i would appreciate any help.

cheers,
Mel

-----------
renaming /home/httpd/htdocs/image.jpg to /home/me/images/2003_03_24_18_14_12.jpg
adding /home/me/images/2003_03_24_18_14_12.jpg to database
[root@cctv cgi-bin]# size is 196378
modified is 20030324181412
filename is 2003_03_24_18_14_12.jpg
Use of uninitialized value in concatenation (.) or string at renamerr.pl line 99
.
Failed to get the info
$file is: at renamerr.pl line 99.
-----------------

The script is as follow:
----------------

#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use Date::Manip;

=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 (in24 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");
infoinsert();
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";
print "adding $new_dir/$stamp.$suffix to database\n";
my $single_string = $new_dir . '/' . $stamp . '.' . $suffix;
infoinsert ($single_string);
}



############################################################################
#
# Connect to Database Named cctvimages on the localhost with the root user
# $dbh=DBI->connect(DBI:mysql;$database", $user, $password);
# and insert info about the file given as the argument $_[0];
############################################################################
#




sub infoinsert
{
my ($file) = @_;
die"Failed to get the info\n\$file is: $file" if not defined $file;
my $dbh = DBI->connect("DBI:mysql:dbname=cctvimages;host=localhost","root",
"**********", {'RaiseError' => 1});
my $size;
my $mtime;
my $secs;
($size, $secs) = (stat ($file))[7,9];
$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);

$sth->execute or die"Execute fails: $DBI::errstr\n";
$sth->finish;

$dbh->disconnect;
}
 
Old 03-26-2003, 01:48 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
well, i dont know whtat line 99 of your code is. but as a matter a fact the variable $file doesnt have a value in there.
maybe u have to declare it outside of a sub to make it global. or maybe it is not in @_ as assumed in the last sub.
cheers, jens

Last edited by j-ray; 03-26-2003 at 01:55 AM.
 
Old 04-01-2003, 09:59 PM   #3
jw_griffith
LQ Newbie
 
Registered: Mar 2003
Location: Westminster, CO
Distribution: SuSE 8.1
Posts: 15

Rep: Reputation: 0
from the Perl cookbook another way to get file name and directory

use File::Basename;

$base = basename($path);
$dir = dirname($path);
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 11:43 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
loop through string with perl lfur Programming 2 07-03-2004 09:05 AM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 02:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 06:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration