Lua编程-os.execute()在Windows中不起作用 [英] Lua programming - os.execute() is not working in Windows
问题描述
我正在pure-Lua中创建一个函数来扫描目录中的文件并将它们放在另一个文件中.
I'm creating a function in pure-Lua to scan the files from a directory and put they on a another file.
我尝试的命令是:
os.execute( "dir /B C:\\Users\\Fernando\\workspace\\Organizator2\\s1 >
C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt" )
但是...不起作用!我使用其他更简单的命令(例如启动记事本"或"mkdir C:\ test")进行了许多测试,但它们也无法正常工作!更糟糕的是,我直接在提示"中尝试了相同的命令,并且所有命令都是正确的.
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.
我尝试使用io.popen(),但是对于我传递的任何命令(甚至是空字符串!),系统都会返回非法操作".
I tryed use tooo the io.popen(), but it the system returned "illegal operation" for any command i passed (even a empty string!).
这是所有代码:
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 using windows 7 and the Luaforwindows, 5.1, and the LuaEclipse
有人见过这样的问题吗?
Have someone ever seen a problem like this?
推荐答案
我刚刚在我的计算机上测试了您的代码,它可以正常工作(当然,还有我的目录).也许您没有得到预期的结果,因为您的directory
字符串被换行符破坏了,导致:
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:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\
workspace\Organizator2\temp.txt
正确的应该是:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\workspace\Organizator2\temp.txt
请尝试将do end更改为:
Please try changing the do end to:
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt")
这篇关于Lua编程-os.execute()在Windows中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!