How to resolve the algorithm Walk a directory/Recursively step by step in the Red programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Walk a directory/Recursively step by step in the Red programming language
Table of Contents
Problem Statement
Walk a given directory tree and print files matching a given pattern.
Note: This task is for recursive methods. These tasks should read an entire directory tree, not a single directory.
Note: Please be careful when running any code examples found here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Walk a directory/Recursively step by step in the Red programming language
Source code in the red programming language
Red []
walk: func [
"Walk a directory tree recursively, setting WORD to each file and evaluating BODY."
'word "For each file, set with the absolute file path."
directory [file!] "Starting directory."
body [block!] "Block to evaluate for each file, during which WORD is set."
/where
rules [block!] "Parse rules defining file names to include."
][
foreach file read directory [
if where [if not parse file rules [continue]]
either dir? file: rejoin [directory file] [walk item file body] [
set 'word file
do body
]
]
]
rules: compose [
any (charset [#"A" - #"Z"])
".TXT"
]
walk/where file %/home/user/ [print file] rules
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Fractal tree step by step in the Java programming language
You may also check:How to resolve the algorithm Five weekends step by step in the Harbour programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the R programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Delphi programming language