Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that is copying a file from my Windows 2000 Web server to another Info Server twice a day.
The script currently sits on the Windows 2000 Web server where the source file is located and is run off the Task Scheduler twice a day.

I would like to put the script on my Solaris 7 server and use cron to schedule the copy. How can I find the File (myfile) from the Unix server? I know I cant call it what it currently is -> C:\\webServer\\myfile.doc

#How do I name the absolute path? my $sourceFile = "C:\\webServer\\myfile.doc"; my $destinationFile = "\\\\myInfoServer\\dirOne\\"; system("copy $sourceFile $destinationFile > nul") == 0 || die "File d +id not copy: $!"; #will change the copy command to cp when I put it on Unix
Please advise.

Replies are listed 'Best First'.
Re: Copy file from Windows server to antoher windows server
by jcoxen (Deacon) on May 03, 2006 at 19:50 UTC
    I had to do a similar thing several months ago - copy a database file from one Windows box to another using a script on a Sun server. Here's my solution...
    #! /usr/bin/perl # # Program: mv_mdb_files.pl # Author: Jack Coxen <Jack.Coxen@TelCove.com> # # Orig: July 22, 2005 # Purpose: Move Access .mdb files from "\\svr1\source_share\sourc +e_dir\Database.mdb" # to "\\svr2\dest_share\dest_dir\Database.mdb" # # Default use statements use strict; use warnings; #use diagnostics; # Uncomment this for development ONLY!!! # Other use statements use Filesys::SmbClientParser; # Perl client to reach + Samba resources with smbclient use Net::SMTP; # Simple Mail Transfer + Protocol Client # Set DEBUG Messages on or off my $DEBUG = 0; our $src; # Setup variables my $recipient = "name\@company.com"; # Mail recipient my $admin = "admin\@company.com"; # Administrator my $now = gmtime; # Grab the current tim +e # Setup smbclient my $smb_src = new Filesys::SmbClientParser; $smb_src->Auth("/usr/local/cap-files/Master/.smbpw-svr1"); $smb_src->Host("svr1_name"); $smb_src->Share("share_name"); my $smb_dest = new Filesys::SmbClientParser; $smb_dest->Auth("/usr/local/cap-files/Master/.smbpw-svr2"); $smb_dest->Host("svr2_name"); $smb_dest->Share("share_name"); # Setup the SMTP client my $smtp = Net::SMTP->new( Host => 'mail_svr.company.com', Timeout => 30, Debug => $DEBUG, ); # # Main Program # # Copy files from $smb_src to $smb_dest print "cd source_dir\n" if $DEBUG; $smb_src->cd('source_share\\source_dir') or die "Cannot execute cd source_dir", $smb_src->err; print "Copying Database.mdb from svr1\n" if $DEBUG; $smb_src->get('Database.mdb') or die "Cannot get file", $smb_src->err; print "cd dest_dir\n" if $DEBUG; $smb_dest->cd('dest_share\\dest_dir or die "Cannot execute cd dest_dir", $smb_dest->err; print "Copying Database.mdb to svr2\n" if $DEBUG; $smb_dest->put ('Database.mdb') or die "Cannot put file", $smb_dest->err; # Email a run notification to $recipient and cc $admin $smtp->mail('script_owner@company.com'); $smtp->recipient($recipient,$admin, {SkipBad => 1}); $smtp->data(); $smtp->datasend("To: $recipient\n"); $smtp->datasend("CC: $admin\n"); $smtp->datasend("Date: $now\n"); $smtp->datasend("From: Script Owner\n"); $smtp->datasend("Subject: mv_mdb_files.pl\n"); $smtp->datasend("\n"); $smtp->datasend("mv_mdb_files.pl was executed at $now\n\n"); $smtp->datasend("The file \"Database.mdb\" was copied from\n"); $smtp->datasend("\\\\svr1\\source_share\source_dir to\n"); $smtp->datasend("\\\\svr2\\dest_share\dest_dir\n\n"); $smtp->datasend("Please verify that the file copied correctly\n"); $smtp->datasend("\n"); $smtp->datasend("Please report any problems to $admin\n"); $smtp->dataend(); $smtp->quit;
    Set this up as a cron job to run whenever. It'll email an "I ran, make sure I ran right" message to you or whoever when it's done. This was just a quick fix I hacked together so I didn't put any serious error checking into it - 'course my 'quick fix' has been running for almost a year now so maybe I should go back and clean things up <adding that to my ToDo List>. ;)

    Jack

Re: Copy file from Windows server to antoher windows server
by davidrw (Prior) on May 03, 2006 at 18:02 UTC
    How can I find the File (myfile) from the Unix server? I know I cant call it what it currently is -> C:\\webServer\\myfile.doc
    Depends entirely on your network config ... does the windows box share that directory? does the unix server mount it? is there another way to pull off the file (e.g. ftp or http)? This question really needs to be directed at the administrator of that specific windows server.
      hi, please share that script which you use for windows server
Re: Copy file from Windows server to antoher windows server
by nimdokk (Vicar) on May 03, 2006 at 18:06 UTC
    Couple of things:

    1) File::Spec (makes using path info easier).

    2) File::Copy (use Perl copy rather than making a system call.

    3) To send a file to Unix, you might want to look at something like Net::FTP and use FTP to transfer the file from one platform to the other.

      Thanks, What the difference between File::Copy and using system copy command? Does the File::Copy use less server processing?
        • File::Copy's copy puts a lot effort in being portable.
        • It's well tested.
        • It doesn't launch a seperate process, so it's faster and uses less memory. Instead, it uses a system call (or Perl code if the OS doesn't have such a call).
        • It can copy from/to file handles instead of just from/to files.
        • Its arguments are not subject to shell interpretation. (The way in which you used system is buggy and unsafe.)
        • It's makes checking for success much easier.
        • It provides meaningful error messages in $!, on most systems.

        On the other hand,

        • Your expression is not portable.
        • It's not well tested. In fact, there are bugs.
        • It launchs *two* seperate processes, so it's slower and uses more memory.
        • It can't copy from/to file handles, just from/to files.
        • Its arguments are subject to shell interpretation (although that can be fixed). The way in which you used system is buggy and unsafe. (Bug 1)
        • It thinks it succeeds even if the copy fails, as long as the shell was successfully loaded. (I think.) (Bug 2)
        • $! does not contain what you think it does here. It definitely does not contain why the copy failed when it does. (Bug 3)
        For one thing, you don't have to spawn (I think that's the right term) a second process if you use Perl's copy instead of the system command. Also, I find it easier to verify that your copy in fact worked correctly (though this can be done with a system call as well I'm sure).