Created
April 28, 2010 09:33
-
-
Save jeromeetienne/381932 to your computer and use it in GitHub Desktop.
scan dir + symlink ok
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stored here for backup