}
} ;
-HELP: recursive-directory
+HELP: recursive-directory-files
{ $values
{ "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" }
{ "paths" "a sequence of pathname strings" }
}
{ $description "Traverses a directory path recursively and returns a sequence of files in a breadth-first or depth-first manner." } ;
+HELP: recursive-directory-entries
+{ $values
+ { "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" }
+ { "directory-entries" "a sequence of directory-entries" }
+}
+{ $description "Traverses a directory path recursively and returns a sequence of directory-entries in a breadth-first or depth-first manner." } ;
+
HELP: find-file
{ $values
{ "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" } { "quot" quotation }
ARTICLE: "io.directories.search" "Searching directories"
"The " { $vocab-link "io.directories.search" } " vocabulary contains words used for recursively iterating over a directory and for finding files in a directory tree." $nl
"Traversing directories:"
-{ $subsection recursive-directory }
+{ $subsection recursive-directory-files }
+{ $subsection recursive-directory-entries }
{ $subsection each-file }
"Finding files:"
{ $subsection find-file }
-USING: io.directories.search io.files io.files.unique
-io.pathnames kernel namespaces sequences sorting tools.test ;
+USING: combinators.smart io.directories
+io.directories.hierarchy io.directories.search io.files
+io.files.unique io.pathnames kernel namespaces sequences
+sorting strings tools.test ;
IN: io.directories.search.tests
[ t ] [
[
10 [ "io.paths.test" "gogogo" make-unique-file ] replicate
current-temporary-directory get [ ] find-all-files
- ] with-unique-directory drop [ natural-sort ] bi@ =
+ ] cleanup-unique-directory [ natural-sort ] bi@ =
] unit-test
[ f ] [
[ f ] [
{ } t [ "asdfasdfasdfasdfasdf" tail? ] find-in-directories
] unit-test
+
+[ t ] [
+ [
+ current-temporary-directory get
+ "the-head" unique-file drop t
+ [ file-name "the-head" head? ] find-file string?
+ ] cleanup-unique-directory
+] unit-test
+
+[ t ] [
+ [ unique-directory unique-directory ] output>array
+ [ [ "abcd" append-path touch-file ] each ]
+ [ [ file-name "abcd" = ] find-all-in-directories length 2 = ]
+ [ [ delete-tree ] each ] tri
+] unit-test
:: iterate-directory-entries ( iter quot -- directory-entry/f )
iter next-directory-entry [
- quot call( obj -- obj ) [ iter quot iterate-directory-entries ] unless*
+ quot call( obj -- obj )
+ [ iter quot iterate-directory-entries ] unless*
] [
f
] if* ; inline recursive
PRIVATE>
: each-file ( path bfs? quot -- )
- setup-traversal [ name>> ] prepose
- iterate-directory-entries drop ; inline
+ setup-traversal iterate-directory drop ;
: each-directory-entry ( path bfs? quot -- )
setup-traversal iterate-directory-entries drop ;
3dup find-file dup [ 2nip nip ] [ drop file-not-found ] if ;
: find-in-directories ( directories bfs? quot -- path'/f )
- '[
- _ [ _ _ find-file-throws ] attempt-all
- ] [
- drop f
- ] recover ;
+ '[ _ [ _ _ find-file-throws ] attempt-all ]
+ [ drop f ] recover ;
: find-all-in-directories ( directories quot -- paths/f )
'[ _ find-all-files ] map concat ;