Question

In Rebol, there are words for directory and file management, like make-dir, what-dir, rename, create-link, etc. But I cannot find a word to simply copy a file to another location or to a newly created file.

A solution is to READ and WRITE. For example, I can do:

>> source: %.bash_history
== %.bash_history
>> target: %nothing
== %nothing
>> write/binary target (read/binary source)

And it works well. But what if I have a file larger than the available memory? Is there any way to copy a file without loading it into memory?

At the moment, I do with a CALL to the underlying OS:

>> call rejoin ["cp " to-string source " " to-string target]

But this is not portable to some different platforms than mine (GNU/Linux Mint): it will run on all Unices, Mac OSX, but not the rest.

I suppose it shouldn't be too hard to write a small function to do this, guessing the running operating system, and adapting the command line accordingly.

So my question: is there already a rebol standard word to copy files? If not, is there a plan to make one, in a module or something?

Was it helpful?

Solution

I don't recall a built-in way to do it aside from what's in the question, but you can do that by using file ports without buffering:

source: open/direct/binary/read %source
target: open/direct/binary/write %target
bytes_per: 1024 * 100
while [not none? data: copy/part source bytes_per][
   insert target data
]
close target
close source

(Note: This answer is for Rebol 2)

OTHER TIPS

You can also use system/version to detect which OS your script runs on:

call rejoin either 3 = system/version/4 [
    ;windows
    [{copy "} to-local-file source {" "} to-local-file target {"}]
] [
    ;others
    ["cp " to-string source " " to-string target]
]

check this script as well http://www.rebol.org/view-script.r?script=environ.r

If there are other cases you can use;

switch/default system/version/4 [
    2 [] ;mac
    3 [] ;win
         ;...
] [
         ;default
]

Also check there, a few other answers for this problem:

Carl implemented something (I'm surprised it is not included in the heart of Rebol):

http://www.rebol.com/article/0281.html

And Patrick was as surprised as you, a decade and some days ago:

http://www.mail-archive.com/rebol-list@rebol.com/msg16473.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top