470,815 Members | 1,321 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Copy files from one directory to another


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..
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
1 3824
4,059 Expert 2GB
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.

Similar topics

1 post views Thread by Antonio Lopez Arredondo | last post: by
11 posts views Thread by Mike | last post: by
12 posts views Thread by samoore33 | last post: by
4 posts views Thread by GS | last post: by
4 posts views Thread by Masta | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.