Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

by Perlbotics (Chancellor)
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001198]
[Discipulus]: good morning choroba! the eu-crew is complete! ;=)
mxb waves
[marto]: time for coffee

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2018-05-21 08:08 GMT
Find Nodes?
    Voting Booth?