Hi I am trying to copyfiles and dirs keeping there permissions intact and copy to multiple destinations which are on a data.txt file
on the txt file i put
test1
test2
So where ami going wrong? -
-
#!/usr/bin/perl
-
use File::Copy;
-
-
my $srcdir = "/var/www/virtual/website/htdocs/drop";
-
-
opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
-
@files = readdir(DIR);
-
close(DIR);
-
-
open (MYFILE, '/var/www/virtual/website/htdocs/data.txt');
-
foreach (<MYFILE>) {
-
chomp;
-
print "$_\n";
-
my $file = @files;
-
my $dest = "/var/www/virtual/website/htdocs/$_";
-
close (MYFILE);
-
-
my $file = @files;
-
my $source = "$srcdir/$file";
-
-
copy($source, $dest) or die "Copy $old -> $dest failed: $!";
-
print "copy completed";
-
}
-
-
-
8 3315
I'm a bit confused.
@files has a lits of files and folders? Do you want to copy all those files and folders to new folder(s) : test1 and test2?
yes that is correct, the destination folder names would be in the text file
I'm a bit confused.
@files has a lits of files and folders? Do you want to copy all those files and folders to new folder(s) : test1 and test2?
OK, well, unfortunately your code is not correct, as you already know since it's not doing what you hoped. I have to run right now, but I'll check back later and see if you still need help.
I am getting this error when doing this code
Unsuccessful stat on filename containing newline at D:/Perl/lib/File/Copy.pm lin
e 95.
Copy d:\prod\test\drop -> d:\prod\test\test1
failed: No such file or directory at copynow.pl line 22. -
#!/usr/bin/perl
-
-
#use warnings;
-
#use strict;
-
-
use File::Copy;
-
-
open (MYFILE, 'd:\\prod\\test\data.txt');
-
for $line (<MYFILE>) {
-
chomp;
-
print "This is the destination $line\n";
-
close (MYFILE);
-
-
my $srcdir = "d:\\prod\\test\\drop";
-
my $dest = "d:\\prod\\test\\$line";
-
-
-
copy($srcdir, $dest) or die "Copy $srcdir -> $dest failed: $!";
-
-
print "\n\n****** script restarting *******\n";
-
sleep 2;
-
}
-
OK, well, unfortunately your code is not correct, as you already know since it's not doing what you hoped. I have to run right now, but I'll check back later and see if you still need help.
I think your last bit of code should be like this: -
#!/usr/bin/perl
-
-
#use warnings;
-
#use strict;
-
-
use File::Copy;
-
-
open (MYFILE, 'd:/prod/test/data.txt') or die "$!";
-
while (<MYFILE>) {
-
chomp;
-
print "This is the destination $_\n";
-
my $srcdir = "d:/prod/test/drop";
-
my $dest = "d:/prod/test/$_";
-
copy($srcdir, $dest) or die "Copy $srcdir -> $dest failed: $!";
-
print "\n\n****** script restarting *******\n";
-
sleep 2;
-
}
-
close(MYFILE);
-
but I am not sure it will work even with the code corrected. Note that you can use forward-slashes in the file paths even though you are probably using windows.
@jonathan184
File::Copy() does not work recursively.
Try this: - use File::Copy;
-
-
$destDirFile = '<file that contains the target directories>';
-
$destRoot = '<destination root dir>';
-
$srcDir = '<src dir>';
-
-
sub fCopy {
-
my($src, $dest) = @_;
-
my($user, $group, $mode) = (stat($src))[4,5,2];
-
my($srcDir, $destDir);
-
-
($destDir = $dest) =~ s!^(.*)/[^/]+$!$1!;
-
($srcDir = $src) =~ s!^(.*)/[^/]+$!$1!;
-
-
mkdir($destDir, (stat($srcDir)[2])) if ( ! -d $destDir );
-
-
copy($src, $dest);
-
chmod($mode, $dest);
-
chown($user, $group);
-
}
-
-
sub rCopy {
-
my($root, $src, $dest) = @_;
-
my($srcPath) = $root . ($src ? '/' : '') . $src;
-
my($destPath) = $dest . ($src ? '/' : '') . $src;
-
my($fd);
-
-
if ( -d $srcPath ) {
-
-
opendir($fd, $srcPath) or die "$!";
-
-
for $file ( readdir($fd) ) {
-
next if ( $file =~ /\.\.?/ );
-
rCopy($root, $src . ($src ? '/' : '') . "$file", "$dest");
-
}
-
-
closedir($fd);
-
}
-
elsif ( -f $srcPath ) {
-
fCopy($srcPath, $destPath);
-
}
-
}
-
-
open (MYFILE, $destDirFile) or die "$!";
-
-
while (<MYFILE>) {
-
chomp;
-
-
print "This is the destination $_\n";
-
-
my($dest) = "$destRoot/$_";
-
-
rCopy($srcDir, undef, $dest);
-
}
-
-
close(MYFILE);
Greetz, Doc
Oops,
should be - chown($user, $group, $src);
;o)
Thank you guys that helped me. I really appreciate it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: RML |
last post by:
hey guys,
i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to...
|
by: rtilley |
last post by:
Hello,
When working with file and dir info recursively on Windows XP. I'm going
about it like this:
for root, dirs, files in os.walk(path):
for f in files:
ADD F to dictionary
for d in...
|
by: lannsjo |
last post by:
I need a program that simultaneously can copy a single file (1 GB) from
my pc to multiple USB-harddrives.
I have searched the internet and only found one program that does this
on...
|
by: Dan |
last post by:
I have an application that I want to use for copying files. My goal is to
copy a files, if a file is in use or not accessible because of security
reasons I want to make note of that file then...
|
by: Hartmut Dippon |
last post by:
Hi all,
I hope somebody can help me with following problem:
I have an application where I can drag&drop files/dirs from within explorer
onto my form.
If multiple files/dirs are selected I...
|
by: Puja |
last post by:
hi all,
how can I copy directory c:\test to a directory c:\backup with all files,
folders and subfolders that are contained within test.
please advice!
thanks
|
by: GS |
last post by:
from the online help, looks like to code step by step to copy content of
directory to another whereas I can use online line code for directory move
or delete.
I am using 2005 express .net 2
I did...
|
by: alivip |
last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word
my code is trying to get all single word and double word (every Token (word) and...
|
by: aRTx |
last post by:
I have try a couple of time but does not work for me
My files everytime are sortet by NAME.
I want to Sort my files by Date-desc.
Can anyone help me to do it?
The Script
<?
/*
ORIGJINALI
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |