473,394 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
2 5054
numberwhun
3,509 Expert Mod 2GB
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
410 Expert 256MB
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

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

Similar topics

3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it...
1
by: huzz | last post by:
I need to write script in c# that will scan directories for files and insert the files and directory names in the database.. I've have two tables tblDir and tblDocs. Example: -Directory1...
2
by: Arjen | last post by:
Hello, I want to make a command line application that search for *.htm* files inside directories and subdirectories. 1. How can I save all the *.htm* files inside directories and...
8
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...
2
by: John Saunders | last post by:
I deploy web applications in what may be an odd manner. For every web site "x", I have an "x2" web site which points to an empty directory. I can then use Copy Project in VS.NET to deploy to the...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
4
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.