7

我需要一种方法来获取运行脚本的路径(包含源文件的目录),但是

(current-directory) 

从不指向那里(在这种情况下是外部驱动器),而是指向某个预定义的位置。

我创建了一个文件来尝试所有的“查找系统路径”,但它们都不是正在运行的文件!球拍文档没有帮助。

#lang web-server/insta
(define (start request)
  (local [{define (build-ul items)
       `(ul ,@(map itemize items))}
      {define (itemize item)
        `(li ,(some-system-path->string (find-system-path item)))}]
(response/xexpr
`(html
  (head (title "Directories"))
  (body (h1 ,"Some Paths")
        (p ,(build-ul special-paths)))))))

(define special-paths (list 'home-dir 
                        'pref-dir 
                        'pref-file 
                        'temp-dir
                        'init-dir 
                        'init-file 
                        ;'links-file ; not available for Linux
                        'addon-dir
                        'doc-dir 
                        'desk-dir 
                        'sys-dir 
                        'exec-file 
                        'run-file
                        'collects-dir 
                        'orig-dir))

目的是用于本地 Web 服务器应用程序(音乐服务器),它将修改包含源文件的目录下的子目录。我将在 U 盘上携带该应用程序,因此当我在安装了 Racket 的机器和操作系统之间携带它时,它需要能够找到自己的目录。

4

2 回答 2

12

简单方法:取运行脚本名,打成完整路径,然后取其目录:

(path-only (path->complete-path (find-system-path 'run-file)))

但您更可能感兴趣的不是用于执行事物的文件(Web 服务器),而是您将代码放入的实际源文件。即,您希望某些资源接近您的源. 一种较旧的方法是:

(require mzlib/etc)
(this-expression-source-directory)

更好的方法是使用“runtime-path”,这是一种定义此类资源的方法:

(require racket/runtime-path)
(define-runtime-path my-picture "pic.png")

这样做更好,因为它还将路径注册为您的脚本所依赖的东西——例如,如果您要将代码打包为安装程序,Racket 也会知道打包该 png 文件。

最后,您可以使用它指向整个目录:

(define-runtime-path HERE ".")
... (build-path HERE "pic.png") ...
于 2013-05-30T18:41:50.470 回答
0

如果你想要绝对路径,那么我认为应该这样做:

(build-path (find-system-path 'orig-dir)
            (find-system-path 'run-file))
于 2013-05-30T18:42:37.960 回答