如何从某个文件夹中获取文件名数组:
local function get_files(path, prepend_path_to_filenames)
if path:sub(-1) ~= '\\' then
path = path..'\\'
end
local pipe = io.popen('dir /b/a-d "'..path..'*.*" 2> nul')
local output = pipe:read'*a'
pipe:close()
-- If your file names contain national characters
-- output = convert_OEM_to_ANSI(output)
local files = {}
for filename in output:gmatch'[^\r\n]+' do
if prepend_path_to_filenames then
filename = path..filename
end
table.insert(files, filename)
end
return files
end
local array_of_files = get_files('C:\\mydir', false)
for _, fn in ipairs(array_of_files) do
print(fn)
end
如果路径不存在(空文件夹和不存在文件夹之间没有区别),将返回空数组。