Hi forro and welcome to the Monastery.

Here are some hints to get you going:

First correct the errors use strict; is giving you. Hint: my.

Do not forget to chomp( @text ); otherwise you could end up searching for somefileame\n.png which most likely does not exist.

Put the filenames from @text into a hash %files_to_find for easy lookup by using a for-loop or map. Decide if you want to create hash-entries for the filename only or for the two variations (filename.txt, filename.png). Creating hash-entries for both names (as keys) makes the wanted()-sub less complex but uses more memory. However, memory-consumption is most likely not an issue here unless you have several hundreds of million files in your remove.txt.

In the wanted()-sub, check if $files_to_find{$filename} exists. Whether $filename is $_ (the filename w/o directory) or $File::Find::name depends on the format of the entries in remove.txt - i.e. if the entries are basenames or also include the full path. You should also check if the file in question is a regular file (-f).
If a match has been found, put the abs. filename in a @remove_these_files list for review and removal in a later step.

Remove files from @remove_these_files after the FF-search. Make a backup and just print out what would have been removed before fiddling around with unlink.

Minor nitpick: The open() doesn't look right. Try open( my $file, '<', "remove.txt") or ..... Then read from <$file>. What you do works, but you create a file-handle file that's visible throughout your program. Usually not a good idea, but IMO tolerable for a small script. It might just be a good idea to get into the 3-args-open-habit early.


In reply to Re: Read .txt file -> append extension -> find on disc -> delete by Perlbotics
in thread Read .txt file -> append extension -> find on disc -> delete by forro

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.