Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Read .txt file -> append extension -> find on disc -> delete

by Perlbotics (Canon)
on Oct 27, 2012 at 12:35 UTC ( #1001198=note: print w/ replies, xml ) Need Help??


in reply to Read .txt file -> append extension -> find on disc -> delete

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.


Comment on Re: Read .txt file -> append extension -> find on disc -> delete
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001198]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2015-07-06 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls