Re: copying each and every file from source directory to destination directory
by shmem (Chancellor) on Oct 20, 2007 at 09:10 UTC
|
Welcome to The Monastery.
Perl comes with a wealth of information: manual pages for the perl core, FAQ compilations and
manual pages for every module. How many pages of those have you read so far?
To read the manual page for File::Copy type
perldoc File::Copy
at the command line prompt. Or click on the above link.
Show yourself round a bit in the Monatsery, browse the Tutorials section, the PerlMonks FAQ,
play with Super Search.
If you want to be spoonfed with information, don't post nodes, but ask in the chatterbox. Thank you.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] |
Re: how to copy each and every file from a source directory to destination directory
by bart (Canon) on Oct 20, 2007 at 06:34 UTC
|
Your loop body is empty. In it, you can put this code:
copy "$source/$file", "$dest$file"
or die "Can't copy file $file: $!";
Also, I think you should check first to see if the inputted string is indeed a directory. Perhaps a check in the opendir call will work.
Plus, you code contains some stray code. Change this
opendir(DIR, $sech $file(@files){
to
opendir(DIR, $source)
or die "Cannot opend the directory '$source': $!";
| [reply] [d/l] [select] |
Re: copying each and every file from source directory to destination directory
by weismat (Friar) on Oct 20, 2007 at 06:55 UTC
|
You may use File:Util as well.
The code may look like this (taken from the docu).
use File::Util;
use File::Copy;
my($f) = File::Util->new();
my(@files) = $f->list_dir($dir,'--files-only');
foreach $file (@files) {
copy ($file, "$targetDir/$file);
}
| [reply] [d/l] |
|
|
hi
thanks for the reply.
i did excecute your code as given and did give source and target directory name i got an error as below.
Can't locate File/Util.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5
+/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_per
+l/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-l
+inux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-mu
+lti /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/p
+erl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl
+/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/li
+b/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5
+/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_p
+erl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i
+386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-th
+read-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /
+usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl
+5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_per
+l/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr
+/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/li
+b/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/p
+erl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./org line 2.
BEGIN failed--compilation aborted at ./org line 2.
| [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
Re: how to a specific group of files from source directory to destination directory
by ahmad (Hermit) on Oct 20, 2007 at 05:53 UTC
|
i cannot see the copy function anywhere in your code
anyway you properly needs something like
#!/usr/bin/perl -w
use strict;
use File::Copy;
# Desitnation directory
my $DesDir = '';
# Source dir name
print "Enter Source dir: ";
chomp(my $Source = <STDIN>);
if (!-d $Source ) {
print "Dir : $Source doesn't exists\n";
exit;
}
opendir(DIR,$Source);
while (my $F = readdir(DIR)) {
next if ($F!~m/\.log$/);
copy("$Source/$F","DesDir/$F");
}
closedir(DIR);
print "Copy Complete\n";
HTH | [reply] [d/l] |
Re: how to a specific group of files from source directory to destination directory
by rminner (Chaplain) on Oct 20, 2007 at 08:03 UTC
|
#!/usr/bin/perl
use File::Copy;
$dest="/home/dpavu2/users1/perl/sra/";
print "the source direcotry name:\n";
chomp(my $source = <STDIN>);
opendir(DIR, $sech $file(@files){
@files = grep { /\.log$/ } readdir (DIR);
foreach $file(@files){
}
closedir (DIR);
i guess you were trying to do the following:
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
use File::Spec;
my $dest="/home/dpavu2/users1/perl/sra/";
print "the source direcotry name: \n";
my $source = '';
chomp($source = <STDIN>);
if (opendir(my $DIR, $source)){
my @files = grep { /\.log$/ } readdir ($DIR);
foreach my $f (@files){
my $src = File::Spec->catfile($source , $f);
my $dst = File::Spec->catfile($dest , $f);
copy($src , $dst) or die "Failed to copy $src to $dest ($!)\n"
+;
}
closedir ($DIR);
} else {
warn "Failed to open '$source ' for reading ($!)\n";
}
The Module File::Copy::Recursive is also quite practical when copying files. You should also always include
use strict;
use warnings;
directives at the beginning of your perl code. If you don't understand an error message then
use diagnostics;
might create error messages which make more sense to you.
Cheers
Roland | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
#!/usr/bin/perl -w
print join(" ", reverse split /\s/), "\n" for (reverse <STDIN>);
...roboticus | [reply] [d/l] [select] |
|
|
i am very thank ful for the help extended same thing i its working how can i reverse the content of each file?? i.e if a file contains lee jones can i have it as jones lee.
I personally believe that since this is a wholly different question, then you should start a new thread. Notice: one new thread, not a hundred like the list time. Before you do so, please pause a little, take a deep breath and review your requirements: your specs as of now are far too poor - are your files expected to only contain strings like "lee jones"? In the IMHO unlikely case that is actually so, you may be content with
print reverse split /(\s+)/;
in conjunction with the $^I special variable. If you only want to replace the fixed string "lee jones" with "jones lee" everywere, then
s/lee jones/jones lee/g;
may be enough. But things may become trickier if spacing is not uniform, case does matter and you want to preserve it, and so on. In any case, you have to tell us.
| [reply] [d/l] [select] |
|
|
| [reply] |