Lua 编程 - os.execute() 在 Windows 中不起作用
我正在 pure-Lua 中创建一个函数来扫描目录中的文件并将它们放在另一个文件中。
我尝试的命令是:
os.execute( "dir /B C:\\Users\\Fernando\\workspace\\Organizator2\\s1 >
C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt" )
但是...不起作用!我用其他更简单的命令做了很多测试,比如“start notepad”或“mkdir C:\test”,但它们也不起作用!更糟糕的是,我直接在提示符中尝试了相同的命令,并且全部正确。
我也尝试使用 io.popen(),但系统对我传递的任何命令返回“非法操作”(甚至是空字符串!)。
这是全部代码:
function ScanDirectory(source, str)
local str = str or "temp.txt"
os.execute("dir /B "..source.." > "..str)
directory = io.open(str,"r")
return directory
end
-- main script
do
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\
\workspace\\Organizator2\\temp.txt")
end
我正在使用 windows 7 和 Luaforwindows、5.1 和 LuaEclipse
有人见过这样的问题吗?
I'm creating a function in pure-Lua to scan the files from a directory and put they on a another file.
The command I tryed was:
os.execute( "dir /B C:\\Users\\Fernando\\workspace\\Organizator2\\s1 >
C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt" )
but... dont works! I did many tests with others simpler commands, like "start notepad" or "mkdir C:\test", and they dont worked too! The worse part is that I tryed this same commands directly in the Prompt, and there is all correct.
I tryed use tooo the io.popen(), but it the system returned "illegal operation" for any command i passed (even a empty string!).
here is the all code:
function ScanDirectory(source, str)
local str = str or "temp.txt"
os.execute("dir /B "..source.." > "..str)
directory = io.open(str,"r")
return directory
end
-- main script
do
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\
\workspace\\Organizator2\\temp.txt")
end
I'm using windows 7 and the Luaforwindows, 5.1, and the LuaEclipse
Have someone ever seen a problem like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚在我的计算机上测试了您的代码,它工作正常(当然,在我的目录中)。也许您没有得到预期的结果,因为您的
directory
字符串被换行符破坏,导致:正确的应该是:
请尝试将 do end 更改为:
I just tested your code on my computer and it works correct (with my directories, of course). Maybe you are not getting the expected result because your
directory
string is broken with an newline char, resulting in:The correct should be:
Please try changing the do end to:
请尝试使用以下语法:
os.execute [["dir /BC:\Users\Fernando\workspace\Organizator2\s1 >
C:\Users\Fernando\workspace\Organizator2\temp.txt"]]
请注意,在这种情况下,反斜杠 (
\
) 不是特殊字符。(Lua在内部使用cstrings,有时它会导致一些奇怪和惊人的结果:P)
Please try it with this syntax:
os.execute [["dir /B C:\Users\Fernando\workspace\Organizator2\s1 >
C:\Users\Fernando\workspace\Organizator2\temp.txt"]]
Please note that the backslash (
\
) is not a special character in this case.(Lua uses cstrings internally, sometimes it leads to some weird and amazing results :P)
您列出的大多数命令似乎都是只能在命令提示符下工作的 shell 命令。尝试直接运行cmd.exe,看看是否有提示,如果有,可以尝试通过/c选项将命令传递给cmd.exe。您也可以尝试不启动记事本,看看是否可以运行。
Most of the commands you listed appear to be shell commands that only work within a command prompt. Try running cmd.exe directly to see if you get a prompt, and if so, you can try passing commands to cmd.exe via the /c option. You could also try notepad without the start to see if that runs.
那行得通。在 win 中使用 Linux 风格的命令根本不是一个好主意 =)
That works. Useing Linux-style commands in win is a bad idea at all =)