获取Lua当前工作目录

发布于 2024-11-07 10:52:03 字数 251 浏览 0 评论 0原文

在 Windows XP SP3 上获取当前工作目录(或获取当前运行的 Lua 文件的目录)的 Lua 是什么?我更喜欢使用LuaFileSystem

我无法使用 os.execute("cd") ,因为 os.execute 始终从我的主目录开始(因此总是产生 C:\Documents 和设置\用户名)。

What's the Lua to get the current working directory on Windows XP SP3 (or to get the directory of the currently-running Lua file)? I prefer not to use LuaFileSystem.

I can't use os.execute("cd") because os.execute always starts from my home directory (and thus always yields C:\Documents and Settings\username).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暮色兮凉城 2024-11-14 10:52:03

也许是一些丑陋的黑客之类的

current_dir=io.popen"cd":read'*l'

maybe some ugly hack like

current_dir=io.popen"cd":read'*l'
合约呢 2024-11-14 10:52:03

默认情况下,Lua 没有“原生”方式来支持“当前目录”的概念,或者实际上是“目录”的概念。

获取当前目录的正确方法是使用提供文件夹支持的库。有几个 - 我推荐 luafilesystem

安装后,您可以通过执行以下命令获取当前目录:

lfs.currentdir()

这适用于 Windows、Linux 和 Mac。

但请注意,这些外部库通常涉及一些二进制包。根据您的设置,您可能必须先编译它才能使用它。

编辑:

请注意,当通过 require 包含文件时,表达式 {...}[1] 返回 require 使用的路径> 指令。它不完全是路径,因为:

  • 它使用点来分隔目录并抑制文件末尾的 .lua
  • 它是相对于lua进程初始化的路径而言的。
  • 它取决于package的配置。 path

但是,如果您需要的只是文件的“类似 require 的路径”(可能需要它旁边的文件),那么您可以通过在文件的最开头执行以下操作来获取它:

local PATH = (...):match("(.+)%.[^%.]+$") or (...)

如果 require 'foo.bar.baz' 需要一个名为 baz.lua 的文件,那么 PATH 将为 foo.bar

Lua by default doesn't have a "native" way of supporting the concept of "current directory", or, in fact, the concept of "directory".

The proper way to get the current directory is using a library that provides folder support. There are several - I recommend luafilesystem.

Once it is installed, you can get the current directory by executing:

lfs.currentdir()

This will work on windows, linux and mac.

Beware though that these external libraries usually involve some binary packages. Depending on your setup, you might have to compile it before being able to use it.

EDIT:

Note that when a file is included via require, then the expression {...}[1] returns the path used by the require directive. It is not exactly the path because:

  • It uses dots to separate directories and supresses the .lua at the end of the file.
  • It is relative to the path from which the lua process was initialized
  • It depends on the configuration of package.path

But if all you need is the "require-like path" of the file (maybe to require files next to it) then you can get it by doing this at the very beginning of the file:

local PATH = (...):match("(.+)%.[^%.]+
quot;) or (...)

If a file called baz.lua is required with require 'foo.bar.baz', then PATH will be foo.bar.

魂归处 2024-11-14 10:52:03

您应该能够使用以下命令获取当前正在运行的 lua 文件路径:

debug.getinfo(1).short_src;

debug.getinfo(1).source;

然后使用正则表达式获取当前目录:

string.gsub(debug.getinfo(1).short_src, "^(.+\\)[^\\]+$", "%1");

编辑:实际上,只有在使用完整路径运行 lua 时才有效。
例如:“lua.exe C:\test\test.lua”而不是“lua.exe test.lua”

You should be able to get the currently-running lua file path with:

debug.getinfo(1).short_src;

or

debug.getinfo(1).source;

and then the current directory with a regex:

string.gsub(debug.getinfo(1).short_src, "^(.+\\)[^\\]+$", "%1");

Edit: actually that only works if you're running your lua with the full path.
e.g.: "lua.exe C:\test\test.lua" and NOT "lua.exe test.lua"

绝影如岚 2024-11-14 10:52:03

我还没有时间测试这个,但是你尝试过 os .getenv 读取windows环境变量?

Windows 有一个当前目录的环境变量: %CD%

os.getenv("CD")

编辑: 在 Windows 7 上测试,当其他环境变量起作用时(即 %USERNAME% 或 %PROGRAMFILES%) CD var 返回 nil

I haven't had time to test this, but have you tried os.getenv to read windows environment variables?

Windows has an environment variable for current directory: %CD%

os.getenv("CD")

Edit: Tested on Windows 7 and while other environment variables work (ie. %USERNAME% or %PROGRAMFILES%) the CD var returns nil

千纸鹤带着心事 2024-11-14 10:52:03

我尝试了所有这些答案,但没有任何效果。我测试了以下脚本,它按预期工作。

io.popen("cd"):read()

I tried all these answers but nothing worked. I tested the following script, and it works as expected.

io.popen("cd"):read()
农村范ル 2024-11-14 10:52:03

您可以使用 alien 呼叫 GetCurrentDirectory in kernel32.dll,但这可能不是您要找的。

You could use alien to call out to GetCurrentDirectory in kernel32.dll, but that's probably not what you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文