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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manishabh77
    New Member
    • Apr 2008
    • 3

    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.

    [CODE=perl]$inputfile1 = "file_index.txt ";
    open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");

    @names = <INP1>;
    close(INP1);

    foreach my $ref (@names)
    {
    chomp($ref);

    use File::Find;

    my $localdir = 'C:\Data\refere nce_material\pe rl_scripts\copy \test_dataset';

    find(
    sub {
    push @files,$File::F ind::name,if /$ref/
    # print $File::Find::na me, "\n" if /\.txt$/

    },
    $localdir);

    }

    foreach my $ref (@files)
    {
    chomp($ref);

    my $source_dir=$re f;
    use File::NCopy;
    my $target_dir = 'test1';
    mkdir($target_d ir) or die "Could not mkdir $target_dir: $!";
    my $cp = File::NCopy->new(recursiv e => 1);
    $cp->copy("$source_ dir/*", $target_dir)
    or die "Could not perform rcopy of $source_dir to $target_dir: $!";

    }[/CODE]
    Last edited by eWish; Apr 3 '08, 11:46 PM. Reason: Please use code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by manishabh77
    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.

    [CODE=perl]$inputfile1 = "file_index.txt ";
    open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");

    @names = <INP1>;
    close(INP1);

    foreach my $ref (@names)
    {
    chomp($ref);

    use File::Find;

    my $localdir = 'C:\Data\refere nce_material\pe rl_scripts\copy \test_dataset';

    find(
    sub {
    push @files,$File::F ind::name,if /$ref/
    # print $File::Find::na me, "\n" if /\.txt$/

    },
    $localdir);

    }

    foreach my $ref (@files)
    {
    chomp($ref);

    my $source_dir=$re f;
    use File::NCopy;
    my $target_dir = 'test1';
    mkdir($target_d ir) or die "Could not mkdir $target_dir: $!";
    my $cp = File::NCopy->new(recursiv e => 1);
    $cp->copy("$source_ dir/*", $target_dir)
    or die "Could not perform rcopy of $source_dir to $target_dir: $!";

    }[/CODE]

    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

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      Originally posted by manishabh77
      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.

      [CODE=perl]$inputfile1 = "file_index.txt ";
      open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");

      @names = <INP1>;
      close(INP1);

      foreach my $ref (@names)
      {
      chomp($ref);

      use File::Find;

      my $localdir = 'C:\Data\refere nce_material\pe rl_scripts\copy \test_dataset';

      find(
      sub {
      push @files,$File::F ind::name,if /$ref/
      # print $File::Find::na me, "\n" if /\.txt$/

      },
      $localdir);

      }

      foreach my $ref (@files)
      {
      chomp($ref);

      my $source_dir=$re f;
      use File::NCopy;
      my $target_dir = 'test1';
      mkdir($target_d ir) or die "Could not mkdir $target_dir: $!";
      my $cp = File::NCopy->new(recursiv e => 1);
      $cp->copy("$source_ dir/*", $target_dir)
      or die "Could not perform rcopy of $source_dir to $target_dir: $!";

      }[/CODE]
      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.
      Code:
        my $cp = File::NCopy->new(recursive => 1);
      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::Rec ursive 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.

      [CODE=perl]
      use strict;
      use File::Find;
      use File::NCopy qw(copy);

      my $inputfile1 = "file_index.txt ";
      my @files=();
      open(INP1, $inputfile1 ) || die("cannot open $inputfile1 in MAIN");

      my @names = <INP1>;
      close(INP1);

      foreach my $ref (@names)
      {
      chomp($ref);


      my $localdir = 'C:/Data';

      find(
      sub {
      push @files,$File::F ind::name if(/$ref/);
      # print $File::Find::na me, "\n" if /\.txt$/

      },
      $localdir);

      }


      my $target_dir = 'test1';
      unless(-e $target_dir) {
      mkdir($target_d ir) or die "Could not mkdir $target_dir: $!";
      }
      my $cp = File::NCopy->new(recursiv e => 1);
      foreach my $ref (@files)
      {
      my $file;
      chomp($ref);
      if($ref=~/^.*?\/([^\/]+)$/) { $file=$1; }
      my $source_dir=$re f;
      if(-f $source_dir) { ##check if source is file
      my $target = $target_dir."/".$file;
      $cp->copy ("$source_di r", "$target") or warn "Could not perform rcopy of $source_dir to $target: $!";
      }
      }

      [/CODE]

      Comment

      Working...