in reply to compare directory list with a text list of filenames

Hi. If you're looking for a simple way to do this in Perl, rather than using system calls and then parsing the results, then you could do it like this:

You say you are OK reading your text file of 'wanted' filenames. When reading it I suggest you use a hash to remember them (maybe called %wanted) - so you'd do ++$wanted{ $filename } for each one (remembering to chomp the filename first if you've just read it as a line from a file).

On to the part you're asking for help with: You can then get a list of all the files in a given directory like this:

# Get a list of all files in a given directory # Note use of the grep to exclude subdirectory names my @files_in_dir = grep { !-d $_ } glob( "myfolder/*" ); # Strip off any leading folder names so that # myfolder/file1.txt then becomes just file1.txt s/.*[\\\/]// for @files_in_dir;

Having done that, you can easily create a new list which is only those files which are on your 'wanted' list:

my @matching_files = grep { $wanted{ $_ } } @files_in_dir;

You've then got a list of filenames in @matching_files which are both in your file, and in the directory you checked.

I suggest you should then look up File::Copy if you're not sure how to do the final part of your task.

Note that this will compare in a case-sensitive way. If you want case-insensitivity, you should lowercase the filenames in the appropriate places above using lc().

Replies are listed 'Best First'.
Re^2: compare directory list with a text list of filenames
by wanlanman (Initiate) on Jul 24, 2007 at 17:08 UTC
    Excellent, thanks very much for the help!