晚上好,
我目前正在开发一个程序,它将文件中的信息传递到数据库中,出于测试目的,我曾经通过IO以经典的方式打开Testfiles:
function reader (file, delimeter)
local f = io.open(file)
for line in f:lines() do
lines[count] = splitty(line, delimeter)
count = count + 1;
end
end
(这部分也包含分离器的第一部分)
但在实际环境中,数据库程序imediatly将文件移动到名称更改为的另一个目录中,例如:
$30$15$2016$09$26$13$27$24$444Z$.Pal.INV.csv
现在我知道了目录,但我无法预测名称,所以我想知道是否有办法在不知道名字的情况下打开文件。 (并在阅读后删除它们)
我有想法使用修改过的链接:
local inputFile = "D:\\Directory\\(*all)"
但失败了。
其他可用信息: 到目前为止,该系统仅在Windows PC上进行规划。 该目录将始终只包含一个准备好的文件,而不包含其他文件。
答案 0 :(得分:2)
您可以使用LuaFileSystem的lfs.dir
迭代器来遍历目录的内容。一个小例子:
local lfs = require("lfs")
local path = "D:\\Directory\\" -- Your directory path goes here.
for filename in lfs.dir(path) do
print(filename) -- Work with filename, i will just print it
end
如果您保留文件记录,您将能够知道哪一个是新文件。如果它只是一个文件,那么它会更容易,您可以使用string
函数检查扩展名。根据我的记忆,迭代器包括..
和.
。 http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_unix-timestamp
答案 1 :(得分:1)
-- directory name and file name should consist of ASCII-7-bit characters only
local dir = [[C:\Temp\New Folder]]
local file = io.popen('dir /b/s/a-d "'..dir..'" 2>nul:'):read"*a":match"%C+"
if not file then
error"No files in this directory"
end
-- print the file name of first file in the directory
print(file) --> C:\Temp\New Folder\New Text Document.txt