Code Comments
Programming Forum and web based access to our favorite programming groups.I am trying to convert a unix script to perl. The script takes 2 command li
ne arguments, source and target. I am unable to get the file copy and renam
e function working properly. Here is what I have that is not working proper
ly.
$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;
}
Post Follow-up to this messageBrian Milbrandt wrote:
> 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. =20
>=20
> $target is the target directory variable
> $source is the source directory variable
>=20
> opendir DH, "/$target";
> foreach $file (readdir DH)
> { printf " the file name is %s\n", $file;
> next unless $file =3D~ \/.abc$/ and !-z $name;
Since you did not provide what really happens, I am assuming that you are =
not getting by ! -z $name. You have not done a chdir, so to get the actual =
file you will need to concatenate "/$target/" . $name for the test.
Wags ;)
> my $newfile =3D /$target//$file;
> $newfile =3D~ s/\.abc$/.xyz/;
> $filecount +=3D 1;
> }
****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************
Post Follow-up to this messageWagner, David --- Senior Programmer Analyst --- WGO wrote: > Brian Milbrandt wrote: I think you really want $file and not $name, but still need to get = right location. Also you need to do either a rename or mv otherwise you = are only changing the name in $newfile, but not actually doing any work. Wags ;) > Since you did not provide what really happens, I am assuming that > you are not getting by ! -z $name. You have not done a chdir, so to > get the actual file you will need to concatenate "/$target/" . $name > for the test. =20 >=20 > Wags ;) >=20 >=20 >=20 >=20 > **************************************** *************** > This message contains information that is confidential > and proprietary to FedEx Freight or its affiliates. > It is intended only for the recipient named and for > the express purpose(s) described therein. > Any other use is prohibited. > **************************************** ***************
Post Follow-up to this messageThe output is a filecount of zero and no files were copied. Brian ----- Original Message ----- From: "Wagner, David --- Senior Programmer Analyst --- WGO" <David.Wagner@freight.fedex.com> To: "Wagner, David --- Senior Programmer Analyst --- WGO" <David.Wagner@freight.fedex.com>; "Brian Milbrandt" <tigger@btm.net>; <beginners@perl.org> Sent: Tuesday, April 19, 2005 5:31 PM Subject: RE: Copy and rename files > Wagner, David --- Senior Programmer Analyst --- WGO wrote: > I think you really want $file and not $name, but still need to get right > location. Also you need to do either a rename or mv otherwise you are > only changing the name in $newfile, but not actually doing any work. > > Wags ;) > > > > -- > To unsubscribe, e-mail: beginners-unsubscribe@perl.org > For additional commands, e-mail: beginners-help@perl.org > <http://learn.perl.org/> <http://learn.perl.org/first-response> > >
Post Follow-up to this message
----- Original Message -----
From: "Brian Milbrandt" <tigger@btm.net>
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";
Post Follow-up to this messageA correction to the code for 1 line below :-(
Chris
> 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.
> #!/usr/bin/perl
> use strict; 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?$/ && -s "$source/$_";
> print "The fileneme is $_\n";
> (my $txt = $_) =~ s/html?$/txt/; copy("$source/$_", "$target/$txt");
> $filecount++;
> }
> close DH;
>
> print "$filecount files copied\n";
>
>
Post Follow-up to this messageI appreciate the help, is there a way to do it by taking the source and target directory's as command line arguments? that is how the unix script is written that I am trying to convert. Thanks. ----- Original Message ----- From: "Chris Charley" <charley@pulsenet.com> To: <beginners@perl.org> Sent: Wednesday, April 20, 2005 3:14 PM Subject: Re: Copy and rename files >A correction to the code for 1 line below :-( > Chris > > > > next unless /html?$/ && -s "$source/$_"; > > > > > -- > To unsubscribe, e-mail: beginners-unsubscribe@perl.org > For additional commands, e-mail: beginners-help@perl.org > <http://learn.perl.org/> <http://learn.perl.org/first-response> >
Post Follow-up to this messageBrian Milbrandt wrote: Please dont' top post :) > I appreciate the help, is there a way to do it by taking the source and > target directory's as command line arguments? that is how the unix script > is written that I am trying to convert. my $source = $ARGV[0]; my $target = $ARGV[1]; may want to add something in case they don't defined them: die "Usage: $0 SOURCE_DIR TARGET_DIR" if !-d $source || $target !~ m/^\w+$/; # or whatever regex is appropriate for your needs (like slashes and periods) HTH :)
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.