Posted By

reboltutorial on 03/02/10


Tagged

RebolAutomation


Versions (?)

How to apply a function to all files in a directory recursively


 / Published in: Bash
 

URL: http://reboltutorial.com/blog/how-to-apply-a-function-to-all-files-in-a-directory-recursively/

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.

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

  1. foreach-file: func [
  2. "Perform function on each file in selected directory recursively"
  3. dir [file! url!] "Directory to look in"
  4. act [function!] "Function to perform (filename is unput to fuction)"
  5. /directory "Perform function also on directories"
  6. /local f files
  7. ][
  8. if not equal? last dir #"/" [
  9. dir: to-rebol-file join dir #"/"
  10. ]
  11. files: attempt [read dir]
  12. either none? files [return][
  13. foreach file files [
  14. f: join dir file
  15. either dir? f [
  16. either directory [
  17. act f
  18. foreach-file/directory f :act
  19. ][
  20. foreach-file f :act
  21. ]
  22. ][act f]
  23. ]
  24. ]
  25. ]

Report this snippet  

You need to login to post a comment.