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().


In reply to Re: compare directory list with a text list of filenames by oxone
in thread compare directory list with a text list of filenames by wanlanman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.