【问题标题】:Rebol2: Change-dir to absolute filepath not workingRebol2:将目录更改为绝对文件路径不起作用
【发布时间】:2016-10-24 16:44:55
【问题描述】:
我正在尝试从配置文件中读取文件路径,然后从该目录中读取。我找不到让它工作的方法,因为由于某种原因 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
【问题讨论】:
标签:
directory
filepath
absolute
rebol
rebol2
【解决方案1】:
它失败了,因为 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
【解决方案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/
【解决方案3】:
找到了有效的解决方法。
changeDirAbsolute: func [input] [
change-dir %/
change-dir input
]
如果有人有更优雅的解决方案,我愿意倾听!
【解决方案4】:
在 Rebol 中,因为代码就是数据,数据就是代码,所以您可以用 Rebol 代码表示您的 .ini 文件。顺便说一句,我和其他许多不以 Windows 为中心的人更喜欢使用 .cfg 作为这些类型文件的扩展名。 .ini 指的是“初始化”,在许多人看来,它指的是系统的引导,但也可以指的是程序的启动。 .cfg 更精确一点,因为它是程序的配置文件。
话虽如此,试试这个:
test.cfg:
test: %/c/users/thompson/downloads
然后,您可以在程序中简单地执行此操作:
>> do %test.cfg
这会自动将文件路径填充到单词“test”中。
在非基于 Windows 的操作系统中,文件路径通常以 / 开头,当它指代文件系统的根级别时。如果不以/开头,则为相对路径(从当前目录开始)。
我希望这会有所帮助!