Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Improve script to check if successful

    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?
    Code:
    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";

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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.

    Code:
    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

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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";

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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.

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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(EFAULT :flock);
    flock(IN, LOCK_EX)

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Code:
    my $listFile = "C:\\theData.xls";
    my $listFile2 = "\\\\InfoServer\\dir\\";
    
    system("copy $listFile $listFile2 > nul") == 0 or die "Copy failed - return code: $?";
    print "Copy completed";

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I appreciate all your time and help on my issue.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •