3

I'm trying to read a filepath from a config file and then read from that directory. I can't find a way to make it work though, because for some reason change-dir never goes to an absolute filepath. Here's a transcript of me trying to make it work on the CLI.

>> test: pick read/lines %test.ini 1
== "test: C/Users/thompson/Downloads/"
>> test: find test " "
== " C/Users/thompson/Downloads/"
>> test: next test
== "C/Users/thompson/Downloads/"
>> test: to file! test
== %C/Users/thompson/Downloads/
>> change-dir test
** Access Error: Cannot open /C/rscratch/C/Users/thompson/Downloads/
** Near: change-dir test
Bo Thompson
  • 359
  • 1
  • 12

4 Answers4

2

It's failing because Rebol does not see

%C/Users/thompson/Downloads/

as an absolute path - it is missing the magic leading slash, so is seen as a relative path. Absolute path is this:

%/C/Users/thompson/Downloads/

So easy fix, if you are sure you do not have that leading slash:

>> test: pick read/lines %test.ini 1
== "test: C/Users/thompson/Downloads/"
>> test: find test " "
== " C/Users/thompson/Downloads/"
>> test: next test
== "C/Users/thompson/Downloads/"
>> test: to file! join "/" test
Thackeray
  • 61
  • 3
2

There are many ways how get to an absolute Rebol file path,

the Rebol way

 test: "test: %/C/Users/thompson/Downloads/"
 select load test [test:]

the linux way

test: "test: /C/Users/thompson/Downloads/"
to-file trim find/tail test "test:"

the Windows way

test: "test: C:/Users/thompson/Downloads/"
to-rebol-file trim find/tail test "test:"

You will always get %/C/Users/thompson/Downloads/

sqlab
  • 6,412
  • 1
  • 14
  • 29
1

Found an effective workaround.

changeDirAbsolute: func [input] [
change-dir %/
change-dir input
]

If anyone has a more elegant solution I'm open to hearing it!

Bo Thompson
  • 359
  • 1
  • 12
1

In Rebol, because code is data and data is code, you can represent your .ini files by Rebol code. Incidentally, I and many others who are not Windows-centric prefer to use .cfg as the extension for these types of files. .ini refers to "initialization" which in many minds refers to the booting of a system, but could also refer to the starting of a program. .cfg is a little more precise in that it is a configuration file for a program.

With that said, try this instead:

test.cfg:

test: %/c/users/thompson/downloads

Then, you can simply do this from within your program:

>> do %test.cfg

That will automatically populate the filepath into the word 'test.

In non-Windows based operating systems, most often a filepath starts with a / when it refers to the root level of the filesystem. If it doesn't start with a /, it is a relative path (starting from the current directory).

I hope this helps!

Respectech
  • 454
  • 4
  • 13