Search the web
Sign In
New User? Sign Up
perl-beginner · Perl Beginners Mailing List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
copy & move function   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#26303 From: "ramesh.govinda" <ramesh.govinda@...>
Date: Sun Jul 20, 2008 2:38 pm
Subject: copy & move function
ramesh.govinda
Offline Offline
Send Email Send Email
 
Hello All,

I have a small query about this code. I had earlier asked you this
problem, but that time I had mentioned the directories in terms of
number s and so it was easier to use foreach loop. But now i need to
create directories which is a mixture of variables and number and then
I need to copy the file and give the respective folder name to the file.

Could anyone let me know if I could go for loop for this code

#!\usr\bin\perl
use warnings;
mkdir("F:/usr/eg/abc", 0777) || print $!;
mkdir("F:/usr/eg/abc/FD", 0777) || print $!;
mkdir("F:/usr/eg/pqt", 0777) || print $!;
mkdir("F:/usr/eg/pqt/FD", 0777) || print $!;
mkdir("F:/usr/eg/sde", 0777) || print $!;
mkdir("F:/usr/eg/sde/FD", 0777) || print $!;
print "Enter the file to copy: ";
$fl = <>;
chomp($fl);
use File::Copy;
copy('F:/usr/eg/z/z.txt','F:/usr/eg/abc/FD');
rename('F:/usr/eg/abc/FD/z.txt','F:/usr/eg/abc/FD/abc.txt');
copy('F:/usr/eg/z/z.txt','F:/usr/eg/pqt/FD');
rename('F:/usr/eg/pqt/FD/z.txt','F:/usr/eg/pqt/FD/pqt.txt');
copy('F:/usr/eg/z/z.txt','F:/usr/eg/sde/FD');
rename('F:/usr/eg/sde/FD/z.txt','F:/usr/eg/sde/FD/sde.txt');

Regards,
Ramesh







#26304 From: "ramesh.govinda" <ramesh.govinda@...>
Date: Sun Jul 20, 2008 3:10 pm
Subject: Re: copy & move function
ramesh.govinda
Offline Offline
Send Email Send Email
 
Following is another code using loop. But I have some problem in the
loop as the file is copied to all my directories and it is unable to
copy the z.txt to the subdirectories. eg: /abc/FD, pqt/FD, /sde/FD.



#!\usr\bin\perl
use warnings;
mkdir("F:/usr/eg/abc", 0777) || print $!;
mkdir("F:/usr/eg/abc/FD", 0777) || print $!;
mkdir("F:/usr/eg/pqt", 0777) || print $!;
mkdir("F:/usr/eg/pqt/FD", 0777) || print $!;
mkdir("F:/usr/eg/sde", 0777) || print $!;
mkdir("F:/usr/eg/sde/FD", 0777) || print $!;
print "Enter the file to copy: ";
$fl = <>;
chomp($fl);
use File::Copy;
opendir DIR, "."; # . is the current directory

while ( $filename = readdir(DIR) )
{
if(-d $filename && $filename ne'.' && $filename ne '.')
{
print "copied $fl to: " , $filename,"\n";
copy($fl,$filename);
}
}
rename('F:/usr/eg/abc/FD/z.txt','F:/usr/eg/abc/FD/abc.txt');
rename('F:/usr/eg/pqt/FD/z.txt','F:/usr/eg/pqt/FD/pqt.txt');
rename('F:/usr/eg/sde/FD/z.txt','F:/usr/eg/sde/FD/sde.txt');

--- In perl-beginner@yahoogroups.com, "ramesh.govinda"
<ramesh.govinda@...> wrote:
>
> Hello All,
>
> I have a small query about this code. I had earlier asked you this
> problem, but that time I had mentioned the directories in terms of
> number s and so it was easier to use foreach loop. But now i need to
> create directories which is a mixture of variables and number and then
> I need to copy the file and give the respective folder name to the file.
>
> Could anyone let me know if I could go for loop for this code
>
> #!\usr\bin\perl
> use warnings;
> mkdir("F:/usr/eg/abc", 0777) || print $!;
> mkdir("F:/usr/eg/abc/FD", 0777) || print $!;
> mkdir("F:/usr/eg/pqt", 0777) || print $!;
> mkdir("F:/usr/eg/pqt/FD", 0777) || print $!;
> mkdir("F:/usr/eg/sde", 0777) || print $!;
> mkdir("F:/usr/eg/sde/FD", 0777) || print $!;
> print "Enter the file to copy: ";
> $fl = <>;
> chomp($fl);
> use File::Copy;
> copy('F:/usr/eg/z/z.txt','F:/usr/eg/abc/FD');
> rename('F:/usr/eg/abc/FD/z.txt','F:/usr/eg/abc/FD/abc.txt');
> copy('F:/usr/eg/z/z.txt','F:/usr/eg/pqt/FD');
> rename('F:/usr/eg/pqt/FD/z.txt','F:/usr/eg/pqt/FD/pqt.txt');
> copy('F:/usr/eg/z/z.txt','F:/usr/eg/sde/FD');
> rename('F:/usr/eg/sde/FD/z.txt','F:/usr/eg/sde/FD/sde.txt');
>
> Regards,
> Ramesh
>





#26305 From: "a_z0_9_blah" <a_z0_9_blah@...>
Date: Sun Jul 20, 2008 7:37 pm
Subject: Re: copy & move function
a_z0_9_blah
Offline Offline
Send Email Send Email
 
--- In perl-beginner@yahoogroups.com, "ramesh.govinda"
<ramesh.govinda@...> wrote:
>
> Following is another code using loop. But I have some problem in
the
> loop as the file is copied to all my directories and it is unable
to
> copy the z.txt to the subdirectories. eg: /abc/FD, pqt/FD, /sde/FD.


Hello Ramesh

Perhaps to following (untested) code will do what you want.

Chris


> #!\usr\bin\perl

#!/usr/bin/perl # those are 'forward' slashes.


> use warnings;

use strict; #this pragma should be declared. It will catch and
report errors in your code.

use File::Path;
use File::Copy;


> mkdir("F:/usr/eg/abc", 0777) || print $!;
> mkdir("F:/usr/eg/abc/FD", 0777) || print $!;
> mkdir("F:/usr/eg/pqt", 0777) || print $!;
> mkdir("F:/usr/eg/pqt/FD", 0777) || print $!;
> mkdir("F:/usr/eg/sde", 0777) || print $!;
> mkdir("F:/usr/eg/sde/FD", 0777) || print $!;

my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD F:/usr/eg/sde/FD];

# no need to specify '0777'. (It is the default)
for my $dir (@dirs) {
mkpath($dir) or die $!;
}


> print "Enter the file to copy: ";
> $fl = <>;
> chomp($fl);

chomp(my $file = <>);

for my $dir (@dirs) {
my ($rename) = $dir =~ m!/(\w+)/FD$!;
copy($file, "$dir/$rename.txt") or die $!;
}



> use File::Copy;
> opendir DIR, "."; # . is the current directory
>
> while ( $filename = readdir(DIR) )
> {
> if(-d $filename && $filename ne'.' && $filename ne '.')
> {
> print "copied $fl to: " , $filename,"\n";
> copy($fl,$filename);
> }
> }
> rename('F:/usr/eg/abc/FD/z.txt','F:/usr/eg/abc/FD/abc.txt');
> rename('F:/usr/eg/pqt/FD/z.txt','F:/usr/eg/pqt/FD/pqt.txt');
> rename('F:/usr/eg/sde/FD/z.txt','F:/usr/eg/sde/FD/sde.txt');
>





#26306 From: "ramesh.govinda" <ramesh.govinda@...>
Date: Sun Jul 20, 2008 10:57 pm
Subject: Re: copy & move function
ramesh.govinda
Offline Offline
Send Email Send Email
 
Hi Chris,

The code works out well. By using loop the code is very short now. The
only line which i could not understand is

my ($rename) = $dir =~ m!/(\w+)/FD$!;

Could you please explain me this syntax.

Thanks in advance

Regards,
Ramesh


--- In perl-beginner@yahoogroups.com, "a_z0_9_blah" <a_z0_9_blah@...>
wrote:
>
> --- In perl-beginner@yahoogroups.com, "ramesh.govinda"
> <ramesh.govinda@> wrote:
> >
> > Following is another code using loop. But I have some problem in
> the
> > loop as the file is copied to all my directories and it is unable
> to
> > copy the z.txt to the subdirectories. eg: /abc/FD, pqt/FD, /sde/FD.
>
>
> Hello Ramesh
>
> Perhaps to following (untested) code will do what you want.
>
> Chris
>
>
> > #!\usr\bin\perl
>
> #!/usr/bin/perl # those are 'forward' slashes.
>
>
> > use warnings;
>
> use strict; #this pragma should be declared. It will catch and
> report errors in your code.
>
> use File::Path;
> use File::Copy;
>
>
> > mkdir("F:/usr/eg/abc", 0777) || print $!;
> > mkdir("F:/usr/eg/abc/FD", 0777) || print $!;
> > mkdir("F:/usr/eg/pqt", 0777) || print $!;
> > mkdir("F:/usr/eg/pqt/FD", 0777) || print $!;
> > mkdir("F:/usr/eg/sde", 0777) || print $!;
> > mkdir("F:/usr/eg/sde/FD", 0777) || print $!;
>
> my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD F:/usr/eg/sde/FD];
>
> # no need to specify '0777'. (It is the default)
> for my $dir (@dirs) {
> mkpath($dir) or die $!;
> }
>
>
> > print "Enter the file to copy: ";
> > $fl = <>;
> > chomp($fl);
>
> chomp(my $file = <>);
>
> for my $dir (@dirs) {
> my ($rename) = $dir =~ m!/(\w+)/FD$!;
> copy($file, "$dir/$rename.txt") or die $!;
> }
>
>
>
> > use File::Copy;
> > opendir DIR, "."; # . is the current directory
> >
> > while ( $filename = readdir(DIR) )
> > {
> > if(-d $filename && $filename ne'.' && $filename ne '.')
> > {
> > print "copied $fl to: " , $filename,"\n";
> > copy($fl,$filename);
> > }
> > }
> > rename('F:/usr/eg/abc/FD/z.txt','F:/usr/eg/abc/FD/abc.txt');
> > rename('F:/usr/eg/pqt/FD/z.txt','F:/usr/eg/pqt/FD/pqt.txt');
> > rename('F:/usr/eg/sde/FD/z.txt','F:/usr/eg/sde/FD/sde.txt');
> >
>





#26307 From: "a_z0_9_blah" <a_z0_9_blah@...>
Date: Mon Jul 21, 2008 12:33 am
Subject: Re: copy & move function
a_z0_9_blah
Offline Offline
Send Email Send Email
 
--- In perl-beginner@yahoogroups.com, "ramesh.govinda"
<ramesh.govinda@...> wrote:
>
> Hi Chris,
>
> The code works out well. By using loop the code is very short now.
The
> only line which i could not understand is
>
> my ($rename) = $dir =~ m!/(\w+)/FD$!;

The left side of the assignment is a list (items within the
parentheses) of 1 item, $rename. This gives list context to the
regular expression matching statement on the right side of the
assignment operator. In this case, only 1 group is being captured -
the '\w+' in the parens in the regular expression.

Here, the regular expression is matching:

/ - a foward slash
(\w+) - 1 or more 'word' characters up to
/ - another forward slash
FD - the final two characters in your path
The dollar sign following 'FD' says that the 'FD' is at the end of
the string, (your path).
So, since the expression on the right side of the assignment
operator is in list context, it will return to the left a list of
all captured items, (only the \w, or word, characters in the only
captured group).

For the 3 paths you have supplied, $rename will be assigned abc, pqt
and sde.

The exclamation symbol '!' is used to begin and end the regular
expression, m!...!

To better understand regular expressions, you should read the docs.
http://perldoc.perl.org/perlretut.html
http://perldoc.perl.org/perlre.html
http://perldoc.perl.org/perlreref.html

I'm sure they will explain better than I can.

Chris

>
> Could you please explain me this syntax.
>
> Thanks in advance
>
> Regards,
> Ramesh
>
>
> --- In perl-beginner@yahoogroups.com, "a_z0_9_blah" <a_z0_9_blah@>
> wrote:
> >
> > --- In perl-beginner@yahoogroups.com, "ramesh.govinda"
> > <ramesh.govinda@> wrote:
> > >
> > > Following is another code using loop. But I have some problem
in
> > the
> > > loop as the file is copied to all my directories and it is
unable
> > to
> > > copy the z.txt to the subdirectories. eg: /abc/FD,
pqt/FD, /sde/FD.
> >
> >
> > Hello Ramesh
> >
> > Perhaps to following (untested) code will do what you want.
> >
> > Chris
> >
> >
> > > #!\usr\bin\perl
> >
> > #!/usr/bin/perl # those are 'forward' slashes.
> >
> >
> > > use warnings;
> >
> > use strict; #this pragma should be declared. It will catch and
> > report errors in your code.
> >
> > use File::Path;
> > use File::Copy;
> >
> >
> > > mkdir("F:/usr/eg/abc", 0777) || print $!;
> > > mkdir("F:/usr/eg/abc/FD", 0777) || print $!;
> > > mkdir("F:/usr/eg/pqt", 0777) || print $!;
> > > mkdir("F:/usr/eg/pqt/FD", 0777) || print $!;
> > > mkdir("F:/usr/eg/sde", 0777) || print $!;
> > > mkdir("F:/usr/eg/sde/FD", 0777) || print $!;
> >
> > my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD
F:/usr/eg/sde/FD];
> >
> > # no need to specify '0777'. (It is the default)
> > for my $dir (@dirs) {
> > mkpath($dir) or die $!;
> > }
> >
> >
> > > print "Enter the file to copy: ";
> > > $fl = <>;
> > > chomp($fl);
> >
> > chomp(my $file = <>);
> >
> > for my $dir (@dirs) {
> > my ($rename) = $dir =~ m!/(\w+)/FD$!;
> > copy($file, "$dir/$rename.txt") or die $!;
> > }
> >
> >
> >
> > > use File::Copy;
> > > opendir DIR, "."; # . is the current directory
> > >
> > > while ( $filename = readdir(DIR) )
> > > {
> > > if(-d $filename && $filename ne'.' && $filename ne '.')
> > > {
> > > print "copied $fl to: " , $filename,"\n";
> > > copy($fl,$filename);
> > > }
> > > }
> > > rename('F:/usr/eg/abc/FD/z.txt','F:/usr/eg/abc/FD/abc.txt');
> > > rename('F:/usr/eg/pqt/FD/z.txt','F:/usr/eg/pqt/FD/pqt.txt');
> > > rename('F:/usr/eg/sde/FD/z.txt','F:/usr/eg/sde/FD/sde.txt');
> > >
> >
>





#26311 From: Ramesh Govinda <ramesh.govinda@...>
Date: Mon Jul 21, 2008 6:23 am
Subject: Re: [PBML] copy & move function
ramesh.govinda
Offline Offline
Send Email Send Email
 
Hi Chris,

Thanks!.That was really awesome explanation. Will checkout the links for further
understanding.

Regards,
Ramesh

--- On Mon, 7/21/08, a_z0_9_blah <a_z0_9_blah@...> wrote:
From: a_z0_9_blah <a_z0_9_blah@...>
Subject: [PBML] Re: copy & move function
To: perl-beginner@yahoogroups.com
Date: Monday, July 21, 2008, 12:33 AM











--- In perl-beginner@ yahoogroups. com, "ramesh.govinda"

<ramesh.govinda@ ...> wrote:

>

> Hi Chris,

>

> The code works out well. By using loop the code is very short now.

The

> only line which i could not understand is

>

> my ($rename) = $dir =~ m!/(\w+)/FD$ !;



The left side of the assignment is a list (items within the

parentheses) of 1 item, $rename. This gives list context to the

regular expression matching statement on the right side of the

assignment operator. In this case, only 1 group is being captured -

the '\w+' in the parens in the regular expression.



Here, the regular expression is matching:



/ - a foward slash

(\w+) - 1 or more 'word' characters up to

/ - another forward slash

FD - the final two characters in your path

The dollar sign following 'FD' says that the 'FD' is at the end of

the string, (your path).

So, since the expression on the right side of the assignment

operator is in list context, it will return to the left a list of

all captured items, (only the \w, or word, characters in the only

captured group).



For the 3 paths you have supplied, $rename will be assigned abc, pqt

and sde.



The exclamation symbol '!' is used to begin and end the regular

expression, m!...!



To better understand regular expressions, you should read the docs.

http://perldoc. perl.org/ perlretut. html

http://perldoc. perl.org/ perlre.html

http://perldoc. perl.org/ perlreref. html



I'm sure they will explain better than I can.



Chris



>

> Could you please explain me this syntax.

>

> Thanks in advance

>

> Regards,

> Ramesh

>

>

> --- In perl-beginner@ yahoogroups. com, "a_z0_9_blah" <a_z0_9_blah@ >

> wrote:

> >

> > --- In perl-beginner@ yahoogroups. com, "ramesh.govinda"

> > <ramesh.govinda@ > wrote:

> > >

> > > Following is another code using loop. But I have some problem

in

> > the

> > > loop as the file is copied to all my directories and it is

unable

> > to

> > > copy the z.txt to the subdirectories. eg: /abc/FD,

pqt/FD, /sde/FD.

> >

> >

> > Hello Ramesh

> >

> > Perhaps to following (untested) code will do what you want.

> >

> > Chris

> >

> >

> > > #!\usr\bin\perl

> >

> > #!/usr/bin/perl # those are 'forward' slashes.

> >

> >

> > > use warnings;

> >

> > use strict; #this pragma should be declared. It will catch and

> > report errors in your code.

> >

> > use File::Path;

> > use File::Copy;

> >

> >

> > > mkdir("F:/usr/ eg/abc", 0777) || print $!;

> > > mkdir("F:/usr/ eg/abc/FD" , 0777) || print $!;

> > > mkdir("F:/usr/ eg/pqt", 0777) || print $!;

> > > mkdir("F:/usr/ eg/pqt/FD" , 0777) || print $!;

> > > mkdir("F:/usr/ eg/sde", 0777) || print $!;

> > > mkdir("F:/usr/ eg/sde/FD" , 0777) || print $!;

> >

> > my @dirs = qw[ F:/usr/eg/abc/ FD F:/usr/eg/pqt/ FD

F:/usr/eg/sde/ FD];

> >

> > # no need to specify '0777'. (It is the default)

> > for my $dir (@dirs) {

> > mkpath($dir) or die $!;

> > }

> >

> >

> > > print "Enter the file to copy: ";

> > > $fl = <>;

> > > chomp($fl);

> >

> > chomp(my $file = <>);

> >

> > for my $dir (@dirs) {

> > my ($rename) = $dir =~ m!/(\w+)/FD$ !;

> > copy($file, "$dir/$rename. txt") or die $!;

> > }

> >

> >

> >

> > > use File::Copy;

> > > opendir DIR, "."; # . is the current directory

> > >

> > > while ( $filename = readdir(DIR) )

> > > {

> > > if(-d $filename && $filename ne'.' && $filename ne '.')

> > > {

> > > print "copied $fl to: " , $filename,"\ n";

> > > copy($fl,$filename) ;

> > > }

> > > }

> > > rename('F:/usr/ eg/abc/FD/ z.txt','F: /usr/eg/abc/ FD/abc.txt' );

> > > rename('F:/usr/ eg/pqt/FD/ z.txt','F: /usr/eg/pqt/ FD/pqt.txt' );

> > > rename('F:/usr/ eg/sde/FD/ z.txt','F: /usr/eg/sde/ FD/sde.txt' );

> > >

> >

>





























[Non-text portions of this message have been removed]




#26313 From: Ramesh Govinda <ramesh.govinda@...>
Date: Tue Jul 22, 2008 6:25 am
Subject: Re: [PBML] Re: copy & move function
ramesh.govinda
Offline Offline
Send Email Send Email
 
Hello Chris,

I just have a small query adding to my previous problem. I am just curious to
know if I could have this desired output.

My input file is r145_AF_FD_z_HJ.txt

So finally my output should be

/abc/ FD/ r145_AF_FD_abc_HJ.txt
/pqt/ FD/r145_AF_FD_pqt_HJ.txt
/sde/ FD/r145_AF_FD_sde_HJ.txt

I guess we could add the prefix and suffix while defining my ($rename) = $dir =~
m!/(\w+)/FD$ !;
I would like to know if this is right.

Regards,
Ramesh


--- On Sun, 7/20/08, a_z0_9_blah <a_z0_9_blah@...> wrote:
From: a_z0_9_blah <a_z0_9_blah@...>
Subject: [PBML] Re: copy & move function
To: perl-beginner@yahoogroups.com
Date: Sunday, July 20, 2008, 7:37 PM











--- In perl-beginner@ yahoogroups. com, "ramesh.govinda"

<ramesh.govinda@ ...> wrote:

>

> Following is another code using loop. But I have some problem in

the

> loop as the file is copied to all my directories and it is unable

to

> copy the z.txt to the subdirectories. eg: /abc/FD, pqt/FD, /sde/FD.



Hello Ramesh



Perhaps to following (untested) code will do what you want.



Chris



> #!\usr\bin\perl



#!/usr/bin/perl # those are 'forward' slashes.



> use warnings;



use strict; #this pragma should be declared. It will catch and

report errors in your code.



use File::Path;

use File::Copy;



> mkdir("F:/usr/ eg/abc", 0777) || print $!;

> mkdir("F:/usr/ eg/abc/FD" , 0777) || print $!;

> mkdir("F:/usr/ eg/pqt", 0777) || print $!;

> mkdir("F:/usr/ eg/pqt/FD" , 0777) || print $!;

> mkdir("F:/usr/ eg/sde", 0777) || print $!;

> mkdir("F:/usr/ eg/sde/FD" , 0777) || print $!;



my @dirs = qw[ F:/usr/eg/abc/ FD F:/usr/eg/pqt/ FD F:/usr/eg/sde/ FD];



# no need to specify '0777'. (It is the default)

for my $dir (@dirs) {

mkpath($dir) or die $!;

}



> print "Enter the file to copy: ";

> $fl = <>;

> chomp($fl);



chomp(my $file = <>);



for my $dir (@dirs) {

my ($rename) = $dir =~ m!/(\w+)/FD$ !;

copy($file, "$dir/$rename. txt") or die $!;

}



> use File::Copy;

> opendir DIR, "."; # . is the current directory

>

> while ( $filename = readdir(DIR) )

> {

> if(-d $filename && $filename ne'.' && $filename ne '.')

> {

> print "copied $fl to: " , $filename,"\ n";

> copy($fl,$filename) ;

> }

> }

> rename('F:/usr/ eg/abc/FD/ z.txt','F: /usr/eg/abc/ FD/abc.txt' );

> rename('F:/usr/ eg/pqt/FD/ z.txt','F: /usr/eg/pqt/ FD/pqt.txt' );

> rename('F:/usr/ eg/sde/FD/ z.txt','F: /usr/eg/sde/ FD/sde.txt' );

>





























[Non-text portions of this message have been removed]




 
Advanced
Add to My Yahoo!      XML What's This?

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help