Solved

Copy multiple files using perl

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

770 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