Lua 编程 - os.execute() 在 Windows 中不起作用

发布于 2024-10-17 10:14:38 字数 900 浏览 4 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(4

深海少女心 2024-10-24 10:14:38

我刚刚在我的计算机上测试了您的代码,它工作正常(当然,在我的目录中)。也许您没有得到预期的结果,因为您的 directory 字符串被换行符破坏,导致:

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 更改为:

local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1" 
local directory  = ScanDirectory(source, "C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt") 

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

The correct should be:

dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\workspace\Organizator2\temp.txt

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") 
江南烟雨〆相思醉 2024-10-24 10:14:38

请尝试使用以下语法:

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)

烟燃烟灭 2024-10-24 10:14:38

您列出的大多数命令似乎都是只能在命令提示符下工作的 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.

花期渐远 2024-10-24 10:14:38
os.execute('cmd.exe /c dir /B C:\\> C:\\test.txt')

那行得通。在 win 中使用 Linux 风格的命令根本不是一个好主意 =)

os.execute('cmd.exe /c dir /B C:\\> C:\\test.txt')

That works. Useing Linux-style commands in win is a bad idea at all =)

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