RosettaCodeData/Task/Walk-a-directory-Recursively/Sidef/walk-a-directory-recursively.sidef
2017-09-25 22:28:19 +02:00

22 lines
438 B
Text
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func traverse(Block callback, Dir dir) {
dir.open(\var dir_h) || return nil
 
dir_h.entries.each { |entry|
if (entry.is_a(Dir)) {
traverse(callback, entry)
} else {
callback(entry)
}
}
}
 
var dir = Dir.cwd
var pattern = /foo/ # display files that contain 'foo'
 
traverse(
{ |file|
if (file.basename ~~ pattern) {
say file
}
} => dir
)