Skip to content

Instantly share code, notes, and snippets.

@zsoumya
Last active August 6, 2020 21:59
Show Gist options
  • Save zsoumya/cafc97d77c64bcf1fdedc5804218f91d to your computer and use it in GitHub Desktop.
Save zsoumya/cafc97d77c64bcf1fdedc5804218f91d to your computer and use it in GitHub Desktop.
Path Iterator In Ruby
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