----- Original Message ----- From: "Brian Milbrandt" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <beginners@perl.org>
Sent: Tuesday, April 19, 2005 6:22 PM
Subject: Copy and rename files



I am trying to convert a unix script to perl. The script takes 2 command line arguments, source and target. I am unable to get the file copy and rename function working properly. Here is what I have that is not working properly.


$target is the target directory variable
$source is the source directory variable

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

Hello Brian,

I did what you wanted to do on my computer, (Windows XP). The code is pasted below, followed by some explanations. You should be able to get the same results by plugging in abc and xyz where I had html and txt. Also, with source and target directories of your choosing.

Chris

#!/usr/bin/perl
use strict; # Always declare this and warnings so perl can catch and report errors
use warnings;
use File::Copy;


my $source = "/Run";
my $target = "/temp";
my $filecount;

opendir DH, $source or die "Unable to open directory $source: $!;
for (readdir DH) {
next unless /html?$/ && -s; # next unless 'html', (or abc), and non-zero file size
print "The fileneme is $_\n";
(my $txt = $_) =~ s/html?$/txt/; # Change file extension to txt, (or xyz). Changed name now in $txt.
copy("$source/$_", "$target/$txt");
$filecount++;
}
close DH;


print "$filecount files copied\n";



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to