Last active
August 6, 2020 21:59
-
-
Save zsoumya/cafc97d77c64bcf1fdedc5804218f91d to your computer and use it in GitHub Desktop.
Path Iterator In Ruby
This file contains hidden or 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
def list_files(path, level) | |
if File.directory?(path) | |
Dir.foreach(path) do |item| | |
if item != '.' && item != '..' && item != '$RECYCLE.BIN' && item != 'System Volume Information' | |
item = File.join(path, item) | |
size = File.size(item) | |
if File.directory?(item) | |
print("#{"\t" * level}#{item}\n") | |
begin | |
list_files(item, level + 1) | |
rescue | |
end | |
else | |
print("#{"\t" * level}#{item} (#{size})\n") | |
end | |
end | |
end | |
end | |
end | |
if __FILE__ == $0 | |
ARGV[0] = ARGV[0] == nil ? 'C:\ruby192': ARGV[0] | |
list_files(ARGV[0], 0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment