rlib/walk_dir.rb
# Recurse through directory and its sub-directories,
# @param directory [String] directory we are recursing through
# @param strip_leadin [String] Remove this string from the front of the directory name before yielding.
# @yield directory, filename [String, String]
Recurse through directory and its sub-directories.
Recurse through directory and its sub - directories .
def walk_dir(directory:, strip_leadin: nil, &block)
  begin
    strip_leadin.gsub!(/\/$/, '') if strip_leadin != nil
    Dir.open(directory).each do |filename|
      next unless filename != '.' && filename != '..' 
      qualified_filename = "#{directory}/#{filename}"
      begin
        stat_record = File.stat(qualified_filename) 
        begin
          if stat_record.symlink? == false 
            if stat_record.directory?
              walk_dir( directory: qualified_filename, strip_leadin: strip_leadin, &block)
            elsif stat_record.file?
              begin
                if strip_leadin.nil?
                  yield qualified_filename, directory, filename
                elsif strip_leadin == directory
                  yield qualified_filename, '.', filename
                else
                  yield qualified_filename, directory.gsub(/^#{strip_leadin}\//, ''), filename
                end
              rescue StandardError => e
                $stderr.puts "yield failed with error: #{e}"
              end
            end
          end
        end
      rescue StandardError => e
        $stderr.puts "Stat of #{qualified_filename} failed with error: #{e}"
      end
    end
  rescue StandardError => e
    $stderr.puts "walk_dir(#{directory}) : #{e}"
  end
end
def walk_dir ( directory : , strip_leadin : nil , & block ) begin strip_leadin . gsub! ( / \/ $ / , '' ) if strip_leadin != nil Dir . open ( directory ) . each do | filename | next unless filename != '.' && filename != '..' qualified_filename = "#{directory}/#{filename}" begin stat_record = File . stat ( qualified_filename ) begin if stat_record . symlink? == false if stat_record . directory? walk_dir ( directory : qualified_filename , strip_leadin : strip_leadin , & block ) elsif stat_record . file? begin if strip_leadin . nil? yield qualified_filename , directory , filename elsif strip_leadin == directory yield qualified_filename , '.' , filename else yield qualified_filename , directory . gsub ( / ^ #{ strip_leadin } \/ / , '' ) , filename end rescue StandardError => e $stderr . puts "yield failed with error: #{e}" end end end end rescue StandardError => e $stderr . puts "Stat of #{qualified_filename} failed with error: #{e}" end end rescue StandardError => e $stderr . puts "walk_dir(#{directory}) : #{e}" end end
Recurse through directory and its sub-directories,
Recurse through directory and its sub - directories
# ignore parent, and current directories. # It is possible for this to cause an exception if symlink points no where. # Otherwise ignore # recurse through sub-directories. # Process files # puts "Completed Dir #{directory}"
directory
directory we are recursing through
directory we are recursing through
strip_leadin
Remove this string from the front of the directory name before yielding.
Remove this string from the front of the directory name before yielding .
block
]
None