By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,230 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,230 IT Pros & Developers. It's quick & easy.

Copy files from one directory to another

P: 1
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;
Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
Your code is full of syntax errors, and it reads just like a classroom assignment.
Mar 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.