Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Copy and rename files
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Milbrandt
04-20-05 01:55 AM


RE: Copy and rename files
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.
 ****************************************
***************


Report this thread to moderator Post Follow-up to this message
Old Post
Wagner, David --- Senior Programmer Analyst --- WG
04-20-05 01:55 AM


RE: Copy and rename files
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 ;)

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


Report this thread to moderator Post Follow-up to this message
Old Post
David --- Senior Programmer Analyst --- WGO Wagner
04-20-05 01:55 AM


Re: Copy and rename files
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>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Milbrandt
04-20-05 08:56 PM


Re: Copy and rename files
----- 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";



Report this thread to moderator Post Follow-up to this message
Old Post
Chris Charley
04-21-05 01:56 AM


Re: Copy and rename files
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";
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Chris Charley
04-21-05 01:56 AM


Re: Copy and rename files
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>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Milbrandt
04-21-05 08:56 AM


Re: Copy and rename files

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

Report this thread to moderator Post Follow-up to this message
Old Post
JupiterHost.Net
04-21-05 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:15 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.