By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,644 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,644 IT Pros & Developers. It's quick & easy.

find files in directories and subdirectories and copy them into a new directory

P: 3
can anyone suggest how I can find files in directories and subdirectories and copy them into a new directory.I tried the following code but it doesnot do the job.

Expand|Select|Wrap|Line Numbers
  1. $inputfile1 = "file_index.txt"; 
  2. open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");
  3.  
  4. @names = <INP1>;
  5. close(INP1);
  6.  
  7.   foreach my $ref (@names)
  8.   {
  9.     chomp($ref);
  10.  
  11.  use File::Find;
  12.  
  13.  my $localdir = 'C:\Data\reference_material\perl_scripts\copy\test_dataset'; 
  14.  
  15.   find( 
  16.     sub { 
  17.            push @files,$File::Find::name,if /$ref/
  18.           # print $File::Find::name, "\n" if /\.txt$/
  19.  
  20.                }, 
  21.     $localdir);
  22.  
  23. }
  24.  
  25. foreach my $ref (@files)
  26.   {
  27.     chomp($ref);
  28.  
  29. my $source_dir=$ref;
  30.     use File::NCopy;
  31.     my $target_dir  = 'test1';
  32.    mkdir($target_dir) or die "Could not mkdir $target_dir: $!";
  33.     my $cp = File::NCopy->new(recursive => 1);
  34.     $cp->copy("$source_dir/*", $target_dir) 
  35.         or die "Could not perform rcopy of $source_dir to $target_dir: $!";
  36.  
  37. }
Apr 3 '08 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
can anyone suggest how I can find files in directories and subdirectories and copy them into a new directory.I tried the following code but it doesnot do the job.

Expand|Select|Wrap|Line Numbers
  1. $inputfile1 = "file_index.txt"; 
  2. open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");
  3.  
  4. @names = <INP1>;
  5. close(INP1);
  6.  
  7.   foreach my $ref (@names)
  8.   {
  9.     chomp($ref);
  10.  
  11.  use File::Find;
  12.  
  13.  my $localdir = 'C:\Data\reference_material\perl_scripts\copy\test_dataset'; 
  14.  
  15.   find( 
  16.     sub { 
  17.            push @files,$File::Find::name,if /$ref/
  18.           # print $File::Find::name, "\n" if /\.txt$/
  19.  
  20.                }, 
  21.     $localdir);
  22.  
  23. }
  24.  
  25. foreach my $ref (@files)
  26.   {
  27.     chomp($ref);
  28.  
  29. my $source_dir=$ref;
  30.     use File::NCopy;
  31.     my $target_dir  = 'test1';
  32.    mkdir($target_dir) or die "Could not mkdir $target_dir: $!";
  33.     my $cp = File::NCopy->new(recursive => 1);
  34.     $cp->copy("$source_dir/*", $target_dir) 
  35.         or die "Could not perform rcopy of $source_dir to $target_dir: $!";
  36.  
  37. }

Sorry, I don't know the answer but hopefully one of our experts will be able to assist you.

My appologies for the delay in getting an answer to your question.

Regards,

Jeff
Apr 17 '08 #2

nithinpes
Expert 100+
P: 410
can anyone suggest how I can find files in directories and subdirectories and copy them into a new directory.I tried the following code but it doesnot do the job.

Expand|Select|Wrap|Line Numbers
  1. $inputfile1 = "file_index.txt"; 
  2. open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");
  3.  
  4. @names = <INP1>;
  5. close(INP1);
  6.  
  7.   foreach my $ref (@names)
  8.   {
  9.     chomp($ref);
  10.  
  11.  use File::Find;
  12.  
  13.  my $localdir = 'C:\Data\reference_material\perl_scripts\copy\test_dataset'; 
  14.  
  15.   find( 
  16.     sub { 
  17.            push @files,$File::Find::name,if /$ref/
  18.           # print $File::Find::name, "\n" if /\.txt$/
  19.  
  20.                }, 
  21.     $localdir);
  22.  
  23. }
  24.  
  25. foreach my $ref (@files)
  26.   {
  27.     chomp($ref);
  28.  
  29. my $source_dir=$ref;
  30.     use File::NCopy;
  31.     my $target_dir  = 'test1';
  32.    mkdir($target_dir) or die "Could not mkdir $target_dir: $!";
  33.     my $cp = File::NCopy->new(recursive => 1);
  34.     $cp->copy("$source_dir/*", $target_dir) 
  35.         or die "Could not perform rcopy of $source_dir to $target_dir: $!";
  36.  
  37. }
I could point out three problematic area in your code:
1. loading module within the foreach loop. A good programming practice is to load all the modules at the beginning of script unless you have a specific purpose to do otherwise.

2. Declaration of new $cp object within foreach loop with 'my' scope modifier.
Expand|Select|Wrap|Line Numbers
  1.   my $cp = File::NCopy->new(recursive => 1);
  2.  
In this case, $cp object will be created afresh with each iteration of loop that is not what you want.

3. The arguments for copy function should be either both directories or both files. It can't be a combination of this.

Also, if you search in CPAN, you will know that usage of File::NCopy module is deprecated and use of File::Copy::Recursive is suggested.

If all you want is to search for files in folder & subfolders in source directory and copy them to destination directory, the following modified code will do the job.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use File::Find;
  3. use File::NCopy qw(copy);
  4.  
  5. my $inputfile1 = "file_index.txt"; 
  6. my @files=();
  7. open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");
  8.  
  9. my @names = <INP1>;
  10. close(INP1);
  11.  
  12.   foreach my $ref (@names)
  13.   {
  14.     chomp($ref);
  15.  
  16.  
  17.  my $localdir = 'C:/Data'; 
  18.  
  19.   find( 
  20.     sub { 
  21.            push @files,$File::Find::name if(/$ref/);
  22.           # print $File::Find::name, "\n" if /\.txt$/
  23.  
  24.             }, 
  25.     $localdir);
  26.  
  27. }
  28.  
  29.  
  30. my $target_dir  = 'test1';
  31. unless(-e $target_dir)   {
  32.    mkdir($target_dir) or die "Could not mkdir $target_dir: $!";
  33. }
  34. my $cp = File::NCopy->new(recursive => 1);  
  35. foreach my $ref (@files)
  36.   {
  37.     my $file;
  38.     chomp($ref);
  39.     if($ref=~/^.*?\/([^\/]+)$/) { $file=$1; }
  40.    my $source_dir=$ref;
  41.    if(-f $source_dir) {   ##check if source is file
  42.     my $target = $target_dir."/".$file;
  43.     $cp->copy ("$source_dir", "$target") or warn "Could not perform rcopy of $source_dir to $target: $!";
  44.  }
  45. }
  46.  
  47.  
Apr 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.