...

View Full Version : Improve script to check if successful



florida
02-09-2006, 06:52 PM
I cant use File::Copy module on my server so I am using a Copy script that works great.
Please advise how I can improve this to check if copy command was successful? Right now the only way I know the copy was successful was by looking at the InforServer to see the file made it.

I tried to put a die command in the copy statement and it gave me a failure message on the copy command. There must a be a way to check if the copy worked?
Also, how do I lock the file (theData.xls) while I am copying it?


use strict;
my $listFile = "C:\\theData.xls";
my $listFile2 = "\\\\InfoServer\\dir";
open(LIST, $listFile) || die "Cant open $listFile : $!";
while(<LIST>)
{
system("copy $listFile $listFile2 > nul");
}
close(LIST);
print "Copy completed";

bazz
02-09-2006, 07:46 PM
well I'm hardly at the stage of being able to advise in perl but I would try to help by suggesting; move the print "completed\n"; to within the while loop but after the instruction to copy.



use strict;
my $listFile = "C:\\theData.xls";
my $listFile2 = "\\\\InfoServer\\dir";
open(LIST, $listFile) || die "Cant open $listFile : $!";
while(<LIST>)
{
system("copy $listFile $listFile2 > nul");
close(LIST);
print "Copy completed";
}


If that doesn't work, someone else will be along soon

bazz

FishMonger
02-09-2006, 07:54 PM
That's an odd approach. Do you realize that it's going to copy the file multiple times. To see what I'm refering to, remove the output redirection to nul and add this in the while loop just before the system call.

print "copy number $.\n";

####

Try it this way:

my $listFile = "C:\\theData.xls";
my $listFile2 = "\\\\InfoServer\\dir\\";

system("copy $listFile $listFile2 > nul") == 0 or die "Copy failed - return code: $?";
print "Copy completed";

florida
02-10-2006, 02:38 PM
Thanks it works very well and quick in the Windows Task Scheduler.

It works so well I am now going to use it to copy a database from the web server to the info server. I assume I should be locking the file (Access database) while the Perl script is copying it:



use strict;
my $listFile = "C:\\theData.mdb";
my $listFile2 = "\\\\InfoServer\\dir\\";

open (IN, "$listFile") || die "Cant open $listFile for reading: $!";
flock(IN, LOCK_EX) || die "Cant get LOCK_SH on $listFile: $!";
print(IN);
system("copy $listFile $listFile2 > nul") == 0 || die "Copy failed.";
print "Copy completed";
close IN || die "Cant close $listFile: $!";


I keep getting error message:
Bareword "LOCK_EX" not allowed while "strict subs"

Please advise.

FishMonger
02-10-2006, 04:33 PM
You haven't imported the LOCK_* constants whick means you need to use their numbers instead of names.

flock(IN, 2)


In order to use the constants, you need to do this

use Fcntl qw(:DEFAULT :flock);
flock(IN, LOCK_EX)

florida
02-10-2006, 04:44 PM
Thanks it now works.

Do you recommend I should even bother with Flock on Windows when copying a small Access Database from one server to another?

Or just leave it this way you originally told me?


my $listFile = "C:\\theData.xls";
my $listFile2 = "\\\\InfoServer\\dir\\";

system("copy $listFile $listFile2 > nul") == 0 or die "Copy failed - return code: $?";
print "Copy completed";

FishMonger
02-10-2006, 05:56 PM
No, I wouldn't use flock. If you were writting to the file, without question, I'd use flock. In this case, it doesn't make any sense and the process of locking the file takes longer than the copy process.

quoted from the flock documentation

Note that the fcntl(2) emulation of flock(3) requires that
FILEHANDLE be open with read intent to use LOCK_SH and requires
that it be open with write intent to use LOCK_EX.

florida
02-10-2006, 06:13 PM
Thanks, I appreciate all your time and help on my issue.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum