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]