I found foreach-file a fantastic script from Rebolek on Rebol.org that I slightly modified to make it more tolerant to directory path parameter which would not end with “/” character:
foreach-file: func [
"Perform function on each file in selected directory recursively"
dir [file! url!] "Directory to look in"
act [function!] "Function to perform (filename is unput to fuction)"
/directory "Perform function also on directories"
/local f files
][
if not equal? last dir #"/" [
dir: to-rebol-file join dir #"/"
]
files: attempt [read dir]
either none? files [return][
foreach file files [
f: join dir file
either dir? f [
either directory [
act f
foreach-file/directory f :act
][
foreach-file f :act
]
][act f]
]
]
]
To use it, copy and paste the code above in Rebol’s console and then copy and paste the code below to print all the files names in Current and then in Windows directory:
print-file-name: func[file][
print file
]
foreach-file %. :print-file-name
foreach-file %/C/Windows :print-file-name
The “:” before the function call does act like a function pointer in some traditional languages like C++ though in truth it is cloning (duplicating) the function (that is if you change the copy, the original version is unchanged).
Output sample should be like this:

For real world applications, you can easily create for example the same kind of utility as Picky Basket to collect files from several directories and copy or move them to the same directory. Or use it with this file timestamp edit function to set the date of all files in a directory.
But in a future lesson, we’ll see a more complex use of this library to backup all files and subdirectories in a directory to a ftp server.
















Tags: Directory, File, function pointer, library, Recursivity