Re: OT: finding every file not in a list



On Wed, Jan 27, 2010 at 02:16:12PM -0500, Aryeh M. Friedman wrote:
I have a list of files that should be in a dir tree and want to remove
any files from the tree not in list (i.e. if it is on the list keep it
else rm it)... any quick way to do this?

You should probably use a shell command as recommended by others, but I
decided to write a Ruby script to do what you describe. It assumes you
have a plaintext file full of to-keep filenames with each filename being
an absolute path filename (e.g., /usr/local/bin/xpdf instead of something
like xpdf with no absolute path), one such filename per line, with
nothing else in the file. The script, which I saved as fkeep.rb, looks
like this on the inside:

#!/usr/bin/env ruby

# syntax:
# fkeep.rb <filename> [path]
#
# where:
# <filename> is the file containing paths for files to keep
# [path] is an optional path to where this program should
# start deleting files

losers = Dir["#{Dir.getwd}/**/*"]
keepers = IO.readlines ARGV.shift
startpath = ARGV.shift

if startpath
Dir.chdir startpath
end

keepers.each {|filepath| losers.delete filepath.chomp }

losers.each do |filepath|
unless File.directory?(filepath)
File.delete filepath
end
end


I've done some cursory testing with this, and it seems to work just fine,
but use it only at your own risk.

Note that this will not delete directories, because it felt like too much
work to make it *safely* delete directories. You will have to delete any
empty directories yourself if you use this script as written.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Attachment: pgpNOClSerBkd.pgp
Description: PGP signature



Relevant Pages

  • Re: Excel crashing problem when saving from VB
    ... 'get the current filepath and workbook name ... specified but an IT Incident Title ... any filename you input will not be used") ... If EmailError 1 Then ...
    (microsoft.public.excel.programming)
  • Re: using xml to save/open project data
    ... http://www.TransProCalc.org - Free translation project mgmt software ... set filename tk_getOpenfile ... Exec'ing the script you run it as a separate process, your script won't be able to access its vars. ... generating the xml file is easy enough. ...
    (comp.lang.tcl)
  • Re: Help me find 5 mistakes and than solution to thoes mistakes!
    ... > 8:# This script will create links to files from all of the filename ... > 10:# arguments provided on stdin if no filenames provided on command ...
    (Fedora)
  • Re: MIME - scripting and symbol substitutions
    ... This script is creating a text file with a different filename ... symbol called NEWFILENAME (which works, ... MIME from the command line with all options (so normal DCL ...
    (comp.os.vms)
  • Re: Excel crashing problem when saving from VB
    ... The form works perfectly in Excel 2002 sp2 but will hang in sp3. ... specified but an IT Incident Title ... any filename you input will not be used") ... 'plot the select filepath back in to theExcelsht for future saves ...
    (microsoft.public.excel.programming)