Copy files from one folder to another based on similar file name

I trying to write a script that will copy files from one folder to another based on the file name(similar). As I got Few thousands text files in a folder. But I try to find few hundreds of files out of thousands files. It's takes a lot of time to search it one by one.

Copy seem like a good idea to use in this and then use for to loop through the list of files that I try to find out of thousands. But Copy need a specified name. The problem is I only have part of the file name.

Example of list of files(Content of the text file):

ABCDEF-A01

ADEWSD-B03

ABDDER-C23

Example of filename:

GGI_1409506_ABCDEF-A01.txt,GGI_ADEWSD-B03.txt,DA_ABDDER-C23_12304.txt

I only got the ABCDEF-A01 instead of the full filename.

Expected result:

Able to search through the folder and copy the files to another location that matched according the list of files (one text files).

Anything that you can share? Info/ans/related posts? Thank you so much!

Upvotes: 1

Views: 2189

Answers (3)

Praveen
Praveen

Reputation: 902

Try the below code in perl . When running the program pass the arguments for Source Directory path and Destination Directory path along with the list of filename that need to be searched. If destination directory doesn't exist it will create a folder automatically through the program as shown below :

Code:

use strict;
use warnings;
use File::Copy;
my $source = $ARGV[0];
my $destination = $ARGV[1];
my $listFiles = $ARGV[2];

if(-f $destination)
 {
      print "Already unknown extension of file exists with the same name of directory. So rename the file and run the program";
      exit 0;
    }

if(-d "$destination")
 {
      print "Directory where files need to be copied: $destination\n";
    }
else    
 {
      print "No Directory found and hence created the directory $destination\n";
      mkdir("$destination");
      }

opendir DIR, $source or die "cant open dir";
my @files = grep /(.*?)(\.txt)$/,(readdir DIR);
open my $fh, '<', "$listFiles" or die "Cannot open the file names to search $listFiles - $!";
open my $out,'>', "$ARGV[1]\\NoMatch.txt" or die "Cannot write to the file NoMatch.txt - $!";
my @listFileNames = <$fh>;
my @listFiles = ();
foreach my $InputFiles (@files)
 {
      chomp($InputFiles);
    foreach my $list(@listFileNames)
    {  
    chomp($list);   
     if($InputFiles =~ /$list/isg)
      { 
      print "Files : $InputFiles copying\t";
      copy("$InputFiles","$destination");
      print "Files : $InputFiles copied\n";
      push(@listFiles,$list);     
          }
        }  
      }

my %seen = ();
my $count = 0;
foreach my $list (@listFiles)
 {
     $seen{lc($list)} = 1;
     #print $list . "\n";
      }

foreach my $listnames (@listFileNames)
 {

     if($seen{lc($listnames)})
       {
        }
     else
      {
        if($count ==0)
        {
         print "\nFilenames that did not match the text files are present in the destination folder : NoMatch.txt file " . "\n";
            }
         print $out "$listnames\n";
         $count++;
        }   
    }
close($out);      
close($fh);       
closedir(DIR);

Upvotes: 1

Noble
Noble

Reputation: 77

Hope this helps

    #!/usr/bin/perl -w
    use strict;
    use File::Copy;

    my $sorce_direcrtory = qq{};
    my $new_directory = "";

    opendir(my $dh, $sorce_direcrtory) || die;
    while(readdir $dh) {
        if($_ =~ /[A..Z]+\-[A..Z]\d+/){
            move("$sorce_direcrtory/$_", "$new_directory/$_");
        }
    }
    closedir $dh;

Upvotes: 0

user3795654
user3795654

Reputation: 86

create a batch file and put it in the source folder, with your list of files you want to copy.

for /f %%f in (list.txt) do robocopy c:\source d:\dest %%f

Upvotes: 0

Related Questions