How do you copy the content of folder "A" on server #1 to folder "B" on server #2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Jeffries
    New Member
    • Aug 2010
    • 4

    #1

    How do you copy the content of folder "A" on server #1 to folder "B" on server #2

    I am new at using a scripting luaguage. I am trying o use Perl to copy files from server #1 to a folder on server #2. One of the files being copied to server #2 is a zip file. I need to execute winzip to unzip files to another folder on server #2. Is there away to do this using Perl?
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    First read how to use winzip from command line. (I only have done it using winRAR) Then you can execute this command inside Perl by using backticks.

    Second, you can copy files by reading the data of a file from folder1 into an array and writing the array data back to folder2 in a while-loop . You will need commands "open", "readdir", "print"

    Comment

    • Thomas Jeffries
      New Member
      • Aug 2010
      • 4

      #3
      Thanks for your response. I will try this as you have suggested.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Here is some code to get you started. It copies all files of a directory from sourceDir to destinationDir. It is assumed that these directories already exist before starting the script.

        Code:
        package fileCopy;
        
        use strict;
        use warnings;
        
        # settings
        my $sourceDir = "mySourceDir";
        my $destinationDir = "myDestinationDir";
        
        # read directory
        opendir DIR,"$sourceDir" or die "cannot read $sourceDir!";
        my @list=sort readdir DIR;
        closedir DIR;
        
        # for all fines in directory
        foreach my $file (@list) {
        	next if $file =~ /^\./;
        	print "copying file: $file ...\n";
        	
        	# read file into array
        	open INPUT,"< $sourceDir/$file" or die "Error: cannot open $file for reading!";
        	my @rows = <INPUT>;
        	close INPUT;
        		
        	# write file from array
        	open OUTPUT, "> $destinationDir/$file" or die "error: Cannot open $file for writing!";
        	foreach my $row (@list) {
        		print OUTPUT "$row";
        	}
        	close OUTPUT;
        }
        
        print "End of program!\n";
        This works well for small text files. If you have text files which are bigger than your memory can hold, you can write the line right after reading it:
        Code:
        while (my $record = <INPUT>) {
            print OUTPUT $record;
        }
        If you have binary files, you should probably read them not line by line, but byte by byte, in buffered chunks:

        Code:
        ...
        my $charactersRead = read(INPUT, my $buffer, 512);
        print OUTPUT $buffer;
        ...

        Comment

        Working...