Lua 编程 - os.execute() 在 Windows 中不起作用 [英] Lua programming - os.execute() is not working in Windows

查看:34
本文介绍了Lua 编程 - os.execute() 在 Windows 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在纯 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: est",但它们也不起作用!更糟糕的是,我直接在 Prompt 中尝试了相同的命令,结果全部正确.

but... dont works! I did many tests with others simpler commands, like "start notepad" or "mkdir C: est", 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:UsersFernandoworkspaceOrganizator2s1 > C:UsersFernando
workspaceOrganizator2	emp.txt

正确的应该是:

dir /B C:UsersFernandoworkspaceOrganizator2s1 > C:UsersFernandoworkspaceOrganizator2	emp.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆