• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1875
  • Last Modified:

Copy multiple files using perl

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
nick2307
Asked:
nick2307
  • 2
  • 2
1 Solution
 
Adam314Commented:
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
 
nick2307Author Commented:
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
 
Adam314Commented:
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
 
nick2307Author Commented:
oh....I didn't looked at it...thats for pointing out...thanks a lot...
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now