Solved

Copy multiple files using perl

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

821 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