Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Created April 28, 2010 09:33
Show Gist options
  • Save jeromeetienne/381932 to your computer and use it in GitHub Desktop.
Save jeromeetienne/381932 to your computer and use it in GitHub Desktop.
scan dir + symlink ok
#!/usr/bin/env ruby
# reccursively scan the rootdir directory
# - if a block is given, the fullpath are yielded. They are kept IIF the block return true
# - Find.find and Dir dont follow symlinks. BUT this one does
def dir_scan(rootdir, &block)
results = []
dirnames = []
Dir.entries(rootdir).each do |entry|
next if entry == "."
next if entry == ".."
fullpath = [rootdir, entry].join('/')
keepit = (block_given? ? yield(fullpath) : true)
dirnames.push(fullpath) if File.directory?(fullpath)
results.push(fullpath) if keepit
end
dirnames.each { |dirname|
tmp = dir_scan(dirname) { |fullpath| (block_given? ? yield(fullpath) : true) }
results.concat( tmp )
}
return results
end
if $PROGRAM_NAME == __FILE__
rootdir = "."
abspaths = dir_scan(rootdir) {|fullpath|
# keep only the *.rb
File.extname(fullpath) == ".rb"
}
require "pp"
pp abspaths
end
@jeromeetienne
Copy link
Author

stored here for backup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment