
To copy a binary file the easiest way is to use the read / write functions:
write/binary %target-file.exe read/binary %source-file.exe
But for big files, you may encounter some lack of memory. So if you are in need to copy a big file of 200 Mo like me sometimes, Carl has posted a great script to do so:
REBOL [
Title: "Copy File with Optional Checksum"
Author: "Carl Sassenrath"
License: 'MIT
]
copy-file: func [
"Copy a file. Return WORD for failure or return optional checksum."
from [file!]
dest [file!]
/sum "checksum the data"
/local
data
path
ff ; from file port
tf ; to file port
][
path: split-path dest
foreach [block err-word] [
[make-dir/deep path/1] dir-failed
[ff: open/binary/read/seek from] read-failed
[tf: open/binary/write dest] write-failed
[if sum [sum: open [scheme: 'checksum]]] sum-failed
[
while [not tail? ff] [
print index? ff
data: copy/part ff 100000
insert tail tf data
if sum [insert sum data]
ff: skip ff length? data
]
;print index? ff
] copy-failed
][
if error? try block [
if port? sum [close sum]
if tf [close tf]
if ff [close ff]
return err-word
]
]
data: none
if sum [
update sum
data: copy sum
close sum
]
close tf
close ff
data ; checksum value or none
]
Unfortunately this fails with file of over 1 Gb or even less without apparent reason.


















Hello,
I am new to rebol. I was going through the code sample above but I don’t understand how the foreach loop above is working.
can someone with more expertise in rebol explain what the foreach loop in the example above is doing?
thank you
Hello, I have added an hyperlink on foreach or see below
http://www.rebol.com/docs/words/wforeach.html
I will soon make a list of basic tutorials for rebol in a future article for absolute beginner.
We normally think of a function like foreach as iterating over a block of data. For example:
foreach [name coord] ["Joe" 4x3 "Nate" 1x3 "Tam" 3x1] [print [name "is" coord]]That seems very straightforward– in this example there are only simple values in the block, and a block of code at the end to apply at each iteration. In REBOL, however, code is data, and that is what allows Carl to get fancy with his foreach– he uses it to iterate over and execute code just as if they were the simple values in my example. The expression starts off with:
foreach [block err-word] […and steps through the block in pairs, executing the following code at each step:if error? try block [...Thanks to both Admin and edoc for your help.