Rebol2:将-dir更改为绝对文件路径无法正常工作

时间:2016-10-24 16:44:56

标签: directory filepath absolute rebol rebol2

我试图从配置文件中读取文件路径,然后从该目录中读取。我找不到让它工作的方法,因为由于某种原因,change-dir永远不会进入绝对文件路径。这是我试图让它在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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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:"

Windows方式

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

您将始终获得%/C/Users/thompson/Downloads/

答案 2 :(得分:1)

找到了有效的解决方法。

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

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

答案 3 :(得分:1)

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

话虽如此,试试这个:

test.cfg:

test: %/c/users/thompson/downloads

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

>> do %test.cfg

这将自动将文件路径填充到单词' test。

在非基于Windows的操作系统中,大多数情况下文件路径以/引用文件系统的根级别时开始。如果它不以/开头,则它是一个相对路径(从当前目录开始)。

我希望这有帮助!