0

I want to write a simple program that recurses through a given directory, skips if one of a set of excluded directories / strings is found in the complete path&filename OR a file does not exist / is empty and lists the rest, along with file size and creation time.

My attempt looks like this, but it doesnt work:

dir_ignore=[".AppleDouble",".AppleDB",".AppleDesktop",".DS_Store"]

require 'find'

Find.find('/Volumes/Downloads') { |dir_entry| 
     next if dir_ignore.any? {|skip| dir_entry.include? skip} || !File.exist?(dir_entry)
     dir = ["filename" => dir_entry],["filesize" => File.size(dir_entry)],["creationdate" => File.ctime(dir_entry)]
puts dir
}

The program still lists all the files in an ".AppleDouble" directory and finally crashes on a non-existant file. So obviously my boolean expression does not work...

Why?

Hans Meiser
  • 256
  • 3
  • 11
  • originally posted with && operator instead of || ... changed that – Hans Meiser Feb 03 '16 at 21:06
  • If you want to tell Find to skip a directory and not descend into it you need to call [`Find.prune`](http://ruby-doc.org/stdlib-2.3.0/libdoc/find/rdoc/Find.html#method-c-prune). Beyond that, I recommend breaking your problem up into smaller pieces. Write a method that takes a single filename and returns `true` if it should be skipped; write a method that takes a single filename and prints it "along with file size and creation time"; and so on. Smaller pieces equal smaller problems, so when you run into trouble you can come back and ask more focused questions. – Jordan Running Feb 03 '16 at 21:47
  • Good advice, but i think i just found the answer to the boolean operator question: apparently "||" and "or" work differently, "or" seems to work fine. – Hans Meiser Feb 03 '16 at 21:52
  • http://stackoverflow.com/questions/9622817/ruby-boolean-operator-or-difference – Hans Meiser Feb 03 '16 at 22:29
  • Possible duplicate of [ruby boolean operator or || difference](http://stackoverflow.com/questions/9622817/ruby-boolean-operator-or-difference) – phss Jan 15 '17 at 13:23

0 Answers0