Copy files from one directory to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dkmarni
    New Member
    • Mar 2007
    • 1

    Copy files from one directory to another

    Hi,

    I am trying to do this perl script, but not able to complete it successfully.
    Here is the description what the script has to do..

    Accept two and only two command line arguments. Again, these will be the names of two directories. This time, make sure that the "sending" directory exists (and is, indeed, a directory). Make sure that the "receiving" directory exists, and if not, create it. Then, copy files from the sending directory to the receiving directory. Copy only those files that have a suffix of ".log", and are not empty. That is, only copy the .log files that actually contain some data. When you copy these files, change the suffix of the filenames in the receiving directory from ".log" to ".abc". Again, display a count of the number of files copied. And one last thing: These log files will contain two columns of data. When you copy the files, switch the order of the columns in the files you create in the receiving directory.

    The script i wrote for this is..
    #!/usr/bin/perl
    use strict;
    use warnings;
    #Check to be sure exactly 2 arguments passed to script
    die "Must pass exactly 2 arguments to script" if @ARGV != 2;

    # assign meaningfull names to variables
    my $source = $ARGV[0];
    my $target = $ARGV[1];

    # Check to see if First argument is a directory
    # if not exit

    die "$source is not a directory \n" if !-d $source;

    if !-d $target;
    { # Create Target Directory
    mkdir $target, 0755;
    }

    #copy all files with a .log extension to the target directory
    # renaming the extension to abc

    my $filecount = 0;

    foreach my $entry (readdir DH) {
    my $file = "/$target/$entry";
    print " the file name is $file\n";
    }
    my ($file, $name);
    next unless $file =~ \/.log$/ and !-z $name;
    my $newfile = /$target//$file;
    $newfile =~ s/\.log$/.abc/;
    $filecount += 1;


    # Print the number of files copied
    printf "The number of files copied is: %d\n", $filecount;
    $filecount = 0;
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Your code is full of syntax errors, and it reads just like a classroom assignment.

    Comment

    Working...