CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Copying files from one directory to another

 



bill1234
Novice

Feb 11, 2013, 12:17 PM

Post #1 of 5 (674 views)
Copying files from one directory to another Can't Post

I'm trying to write a script that will read the contents of a directory, match a specific portion of the filename, and copy those files to a new directory. All i've been able to do is to copy 1 file and to the same directory.



Code
my $line = <DATA>; 

while(my $line = <DATA>) {
$line =~/(.+wav)$/;

copy ("$1", "$");
}

__DATA__
audio.wav
sample.wav



Laurent_R
Enthusiast / Moderator

Feb 11, 2013, 2:35 PM

Post #2 of 5 (668 views)
Re: [bill1234] Copying files from one directory to another [In reply to] Can't Post

You give too little information.

Assuming you are on Unix and want to copy all *.wav files from the ./old/ to the ./new/ directory, try something like this (quick code, untested):


Code
#!/usr/bin/perl 
use strict;
use warnings;
use File::Copy;

my @file_list = glob ("./old/*.wav");
chomp @file_list;
foreach my $old_file (@file_list) {
my $new_file = $old_file;
$new_file =~ s{/old/}{/new/}; # these two code lines could be made into one, but it is probably clearer this way for a beginner
print "copying $old_file to $new_file \n"; # useless for the program, just information displayed to the user (may also help debugging)
copy ($old_file, $new_file) or die "File $old_file cannot be copied to $new_file $! \n";
}


Just change your file paths to what you need, whether on Unix, Windows or whatever.


rovf
Veteran

Feb 11, 2013, 11:37 PM

Post #3 of 5 (662 views)
Re: [bill1234] Copying files from one directory to another [In reply to] Can't Post

There are a few oddities in your program. For instance, what does "$" mean? Please add

use strict;
use warnings;

to your program and run it again. Aside from this, you loop executes twice.

A note aside: In the case of your program, the '+' in the regexp is redundant, so you can simplify it to /.wav$/ (i.e. filenames ending in wav and are at least 4 characters long).


bill1234
Novice

Feb 12, 2013, 5:55 AM

Post #4 of 5 (657 views)
Re: [bill1234] Copying files from one directory to another [In reply to] Can't Post

Sorry, i should've given a better example. It's not necessarily for *.wav or *.*, but it would need to match part of a filename, or perhaps read through a text file line by line for a particular type of filename and then copy any matches to a new directory.


Laurent_R
Enthusiast / Moderator

Feb 12, 2013, 12:09 PM

Post #5 of 5 (649 views)
Re: [bill1234] Copying files from one directory to another [In reply to] Can't Post

You can just adapt the script to your needs.

If you need to read a file to get the file names, this is not very complicated, you can get zillions of examples on the Internet.

Try something and ask if you encounter difficulties.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives