module Dir::Walker

Overview

The Dir::Walker module supports the top-down traversal of a set of directories.

For example, to total the size of all files under your home directory, ignoring anything in a "dot" directory (e.g. $HOME/.ssh):

require "dir_walker"

total_size : Int64 = 0

Dir::Walker.walk(ENV["HOME"]) do |path|
  if File.directory? path
    if File.basename(path).starts_with?(".")
      Dir::Walker.prune_path # Don't look any further into this directory.
    end
  else
    total_size += File.size(path)
  end
end

puts total_size.humanize

Extended Modules

Defined in:

dir_walker.cr

Constant Summary

DEFAULT_SORT_PROC = Proc(String, String, Int32).new do |path1, path2| path1 <=> path2 end

Instance Method Summary

Instance Method Detail

def prune_path #

Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be traversaly entered.

See the Find module documentation for an example.


[View source]
def walk(*dirs : String, sort_proc = DEFAULT_SORT_PROC, ignore_error = false, &block : String -> ) #

Calls the associated block with the path of every file and directory listed as arguments, then traversaly on their subdirectories, and so on. Optionally an alternative sort_proc per directory listing can be used. Errors can be ignored also optionally. See the Dir::Walker module documentation for an example.


[View source]