Posted By

reboltutorial on 04/23/10


Tagged

REBOL


Versions (?)

File Utils


 / Published in: R
 

URL: http://reboltutorial.com

  1. copy-dir: func [source dest] [
  2. source: to-rebol-file source
  3. dest: to-rebol-file dest
  4. if not exists? dest [make-dir/deep dest]
  5. foreach file read source [
  6. either find file "/" [
  7. copy-dir source/:file dest/:file
  8. ][
  9. print file
  10. write/binary dest/:file read/binary source/:file
  11. ]
  12. ]
  13. ]
  14.  
  15. Folder?: function [
  16. "Returns the path's folder or enclosing directory."
  17. Path [file! url!]
  18. ] [Name] [
  19. all [
  20. Name: find/last Path File-Name? Path
  21. Path: copy/part Path Name
  22. either empty? Path [none] [Path]
  23. ]
  24. ]
  25.  
  26.  
  27. foreach-file: func [
  28. "Perform function on each file in selected directory recursively"
  29. dir [file! url!] "Directory to look in"
  30. act [function!] "Function to perform (filename is unput to fuction)"
  31. /directory "Perform function also on directories"
  32. /local f files
  33. ][
  34. if not equal? last dir #"/" [
  35. dir: to-rebol-file join dir #"/"
  36. ]
  37. files: attempt [read dir]
  38. either none? files [return][
  39. foreach file files [
  40. f: join dir file
  41. either dir? f [
  42. either directory [
  43. act f
  44. foreach-file/directory f :act
  45. ][
  46. foreach-file f :act
  47. ]
  48. ][act f]
  49. ]
  50. ]
  51. ]
  52.  
  53. copy-file: func [
  54. "Copy a file. Return WORD for failure or return optional checksum."
  55. from [file!]
  56. dest [file!]
  57. /sum "checksum the data"
  58. /local
  59. data
  60. path
  61. ff ; from file port
  62. tf ; to file port
  63. starttime;
  64. endtime;
  65. elapsedtime;
  66. ][
  67. ;from: to-rebol-file from
  68. ;dest: to-rebol-file dest
  69. starttime: now/time
  70. path: split-path dest
  71.  
  72. foreach [block err-word] [
  73. [make-dir/deep path/1] dir-failed
  74. [ff: open/binary/read/seek from] read-failed
  75. [tf: open/binary/write dest] write-failed
  76. [if sum [sum: open [scheme: 'checksum]]] sum-failed
  77. [
  78. while [not tail? ff] [
  79. ;print index? ff
  80. data: copy/part ff 100000
  81. insert tail tf data
  82. if sum [insert sum data]
  83. ff: skip ff length? data
  84. ]
  85. ;print index? ff
  86. ] copy-failed
  87. ][
  88. if error? try block [
  89. if port? sum [close sum]
  90. if tf [close tf]
  91. if ff [close ff]
  92. return err-word
  93. ]
  94. ]
  95.  
  96. data: none
  97. if sum [
  98. update sum
  99. data: copy sum
  100. close sum
  101. ]
  102. close tf
  103. close ff
  104. endtime: now/time
  105. elapsedtime: endtime - starttime
  106. print elapsedtime
  107. data ; checksum value or none
  108. ]

Report this snippet  

You need to login to post a comment.