For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > Copy and rename files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Copy and rename files
Brian Milbrandt

2005-04-19, 8:55 pm

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;
}
Wagner, David --- Senior Programmer Analyst --- WG

2005-04-19, 8:55 pm

Brian 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.
****************************************
***************

David --- Senior Programmer Analyst --- WGO Wagner

2005-04-19, 8:55 pm

Wagner, 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 ;)
[color=darkred]
> 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.
> ****************************************
***************


Brian Milbrandt

2005-04-20, 3:56 pm

The 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>
>
>


Chris Charley

2005-04-20, 8:56 pm


----- 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";


Chris Charley

2005-04-20, 8:56 pm

A 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";
>
>



Brian Milbrandt

2005-04-21, 3:56 am

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.

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>
>


JupiterHost.Net

2005-04-21, 3:56 am



Brian 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 :)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com