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
4

4 回答 4

2

它失败了,因为 Rebol 没有看到

%C/Users/thompson/Downloads/

作为绝对路径 - 它缺少魔术前导斜杠,因此被视为相对路径。绝对路径是这样的:

%/C/Users/thompson/Downloads/

很容易解决,如果你确定你没有那个前导斜杠:

>> 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
于 2016-10-25T03:54:58.307 回答
2

有很多方法可以获取绝对 Rebol 文件路径,

Rebol 方式

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

linux方式

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

视窗方式

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

你总会得到%/C/Users/thompson/Downloads/

于 2016-10-25T05:54:17.420 回答
1

在 Rebol 中,因为代码就是数据,数据就是代码,所以您可以用 Rebol 代码表示您的 .ini 文件。顺便说一句,我和其他许多不以 Windows 为中心的人更喜欢使用 .cfg 作为这些类型文件的扩展名。.ini 指的是“初始化”,在许多人看来,它指的是系统的引导,但也可以指的是程序的启动。.cfg 更精确一点,因为它是程序的配置文件。

话虽如此,试试这个:

测试.cfg:

test: %/c/users/thompson/downloads

然后,您可以在程序中简单地执行此操作:

>> do %test.cfg

这将自动将文件路径填充到单词“test.txt”中。

在非基于 Windows 的操作系统中,文件路径通常以 / 开头,当它指代文件系统的根级别时。如果它不以 / 开头,则它是相对路径(从当前目录开始)。

我希望这有帮助!

于 2016-10-24T23:37:05.613 回答
1

找到了有效的解决方法。

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

如果有人有更优雅的解决方案,我愿意听到!

于 2016-10-24T18:27:33.507 回答