Solved

Copy multiple files using perl

Posted on 2006-11-16
4
1,863 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 250 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now