Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copy multiple files using perl

Posted on 2006-11-16
4
Medium Priority
?
1,874 Views
Last Modified: 2011-09-20
Hi..

I want to copy multiple files to specific location.
I used the Xcopy command but somehow its taking all the files from the source directory instead of copying supplied extension files.

Is there any way to look up in the source sirectory for supplied extension files. Loop over it and copy files...

Please advise.
0
Comment
Question by:nick2307
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 17957312
You can use the File::Find module to find all files with a particular extension in a directory tree.
(if you don't need to include sub directories, it would be even easier...)
0
 

Author Comment

by:nick2307
ID: 17958999
I have done something like this...but its not working.....its not copying files....

use Win32;
use File::Copy;
use File::Find;

open (OUT,">C:\\temp\\files.txt");
find (\&copy,"C:\\temp");
$file_path = "C:\\quality_data";

sub copy
{
    if ((/\.bak$/))
    {
      print OUT "$File::Find::name\n";
      
    }
 
}
close(OUT);

open (IN,"C:\\temp\\files.txt");
@files =<IN>;

foreach (@files)
{
$file = $_;
print "$file\n";
copy($file,$file_path) or Win32::MsgBox("Result file can't be created", 0 + MB_ICONINFORMATION, "Error:Result File");
}
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 1000 total points
ID: 17959549
You have a sub copy, that prints the file names to a file.  This sub is taking precedence over the copy sub exported by the File::Copy module.  So when you call copy at the bottom, it calls your sub again.

Also, you don't need to write to a file, then read it.  You could do the actual copying in the sub called by the find routine...

open (OUT,">C:\\temp\\files.txt");
$file_path = "C:\\quality_data\\";
find (\&copy_if_bak,"C:\\temp");
close(OUT);

sub copy_if_bak
{
    return if $File::Find::name !~ /\.bak$/;    #skip non .bak files
    return if !-f $File::Find::name;                #skip non files (eg: directories)
    print OUT "$File::Find::name\n";
    copy($File::Find::name, $file_path) or Win32::MsgBox("Result file can't be created: $!", 0+ MB_ICONINFORMATION, "Error: Result File");
}

0
 

Author Comment

by:nick2307
ID: 17959699
oh....I didn't looked at it...thats for pointing out...thanks a lot...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question