获取Lua当前工作目录
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许是一些丑陋的黑客之类的
maybe some ugly hack like
默认情况下,Lua 没有“原生”方式来支持“当前目录”的概念,或者实际上是“目录”的概念。
获取当前目录的正确方法是使用提供文件夹支持的库。有几个 - 我推荐 luafilesystem。
安装后,您可以通过执行以下命令获取当前目录:
这适用于 Windows、Linux 和 Mac。
但请注意,这些外部库通常涉及一些二进制包。根据您的设置,您可能必须先编译它才能使用它。
编辑:
请注意,当通过
require
包含文件时,表达式{...}[1]
返回require
使用的路径> 指令。它不完全是路径,因为:.lua
。package的配置。 path
但是,如果您需要的只是文件的“类似 require 的路径”(可能需要它旁边的文件),那么您可以通过在文件的最开头执行以下操作来获取它:
如果
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:
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 therequire
directive. It is not exactly the path because:.lua
at the end of the file.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:
If a file called
baz.lua
is required withrequire 'foo.bar.baz'
, thenPATH
will befoo.bar
.您应该能够使用以下命令获取当前正在运行的 lua 文件路径:
或
然后使用正则表达式获取当前目录:
编辑:实际上,只有在使用完整路径运行 lua 时才有效。
例如:“lua.exe C:\test\test.lua”而不是“lua.exe test.lua”
You should be able to get the currently-running lua file path with:
or
and then the current directory with a regex:
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"
我还没有时间测试这个,但是你尝试过 os .getenv 读取windows环境变量?
Windows 有一个当前目录的环境变量: %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%
Edit: Tested on Windows 7 and while other environment variables work (ie. %USERNAME% or %PROGRAMFILES%) the CD var returns nil
我尝试了所有这些答案,但没有任何效果。我测试了以下脚本,它按预期工作。
I tried all these answers but nothing worked. I tested the following script, and it works as expected.
您可以使用 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.