3

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?

3 Answers 3

4

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)

5
  • Good answer, the proposed code works perfectly fine. Note, though, that this uses a non-standard coding style (bytes_per) which violates standard Rebol coding conventions for word naming. In public Rebol code, I strongly advise to adhere to the published coding conventions and use bytes-per instead.
    – earl
    Commented Oct 16, 2013 at 21:00
  • @earl Said coding style is not enforced; they are merely guides. I choose dashes versus underscores based on how the variable is used. Edits like yours have been brought up on Meta before, and are frowned upon.
    – Izkata
    Commented Oct 17, 2013 at 0:15
  • Thanks for the pointer to meta, looks like the comments are the perfect place to remark on the coding style violation then. Obviously these Rebol naming conventions are not enforced (otherwise we wouldn't have to have this conversation), but that doesn't make them any less useful.
    – earl
    Commented Oct 17, 2013 at 13:32
  • Placing "style" and "violation" in the same sentence exposes the commenter. And we never had to have this "conversation", but for his initiating. Commented Feb 22, 2015 at 2:06
  • Well, I have always named my variables (words) with lowercase, underscore-separated style, like bytes_per. I just find it more readable, convenient, etc. Separation with hyphen is not suitable with my favourite editor's autocompletion, unless I manually switch the syntax for rebol. Anyways, I don't feel that it is that important to stick to the convention, it is not strictly a violation. But I'll try to name my variables (words) according to this rule, from now on.
    – Pierre
    Commented Mar 19, 2015 at 9:35
1

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
]
1

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/[email protected]/msg16473.html

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.