searching directory based on csv file

S

steve

I am a bit stumped on what should be pretty easy. I have a csv file
with filenames in it. I want to read that csv file into an array and
then parse through that array, one file at a time and search a given
directory for that filename. When (the filename will be in the
directory) that file is found it is copied from that location to
another given location.

Thanks for any help!
 
A

A. Sinan Unur

I am a bit stumped on what should be pretty easy. I have a csv file
with filenames in it. I want to read that csv file into an array and
then parse through that array, one file at a time and search a given
directory for that filename. When (the filename will be in the
directory) that file is found it is copied from that location to
another given location.

perldoc -f -X

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
T

Tad J McClellan

steve said:
I am a bit stumped on what should be pretty easy. I have a csv file
with filenames in it. I want to read that csv file into an array and


perldoc Text::CSV
perldoc Text::CSV_XS

then parse through that array, one file at a time


See the "Foreach Loops" section in

perldoc perlsyn

and search a given
directory for that filename.


perldoc -f glob
perldoc -f opendir
perldoc -f readdir

When (the filename will be in the
directory) that file is found it is copied from that location to
another given location.


perldoc File::Copy
 
J

John W. Krahn

steve said:
I am a bit stumped on what should be pretty easy. I have a csv file
with filenames in it. I want to read that csv file into an array and
then parse through that array, one file at a time and search a given
directory for that filename. When (the filename will be in the
directory) that file is found it is copied from that location to
another given location.

Thanks for any help!

This is an example of what may work for you but it is UNTESTED:

#!/usr/bin/perl
use warnings;
use strict;

use File::Copy;


my $csv_file = 'file.csv';
my $search_dir = 'some/dir';
my $new_dir = 'some/where/else';


open my $CSV, '<', $csv_file or die "Cannot open '$csv_file' $!";

while ( <$CSV> ) {
chomp;
my $file = ( split /,/ )[ 3 ]; # file name is the fourth field

if ( -e "$search_dir/$file" ) {
copy( "$search_dir/$file", "$new_dir/$file" ) or die "Cannot
copy '$search_dir/$file' $!";
}
}

close $CSV;

__END__




John
 
J

Jürgen Exner

There are several modules on CPAN which handle CSV quite nicely,
including all the oddities of CSV like quotes and escapes. No need to
reinvent the wheel.

perldoc perlsyn, check for the 'for' loop

perldoc -f -X

perldoc File::Copy

Which part do you have trouble with?

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,110
Members
47,703
Latest member
robert.marryson

Latest Threads

Top