【发布时间】:2020-02-07 04:04:40
【问题描述】:
我正在尝试使用 lua 文件从目录中仅复制最新文件。 最新文件意味着:取决于修改时间/创建时间。 我该怎么做?
【问题讨论】:
-
你的操作系统是什么?
-
我会推荐你使用LuaFileSystem。
标签: lua last-modified
我正在尝试使用 lua 文件从目录中仅复制最新文件。 最新文件意味着:取决于修改时间/创建时间。 我该怎么做?
【问题讨论】:
标签: lua last-modified
参考这个问题:How can I get last modified timestamp in Lua
您也许可以利用io.popen 函数执行shell 命令来获取文件名。似乎没有公开文件系统元数据或统计信息的内置函数。这样的事情可能会奏效:
local name_handle = io.popen("ls -t1 | head -n 1")
local filename = name_handle:read()
我不熟悉 Lua,但也许这会有所帮助。我想一旦你有了最新文件的名称,你就可以使用其他 IO 函数来进行复制。
【讨论】:
local function get_last_file_name(directory)
local command = 'dir /a-d /o-d /tw /b "'..directory..'" 2>nul:'
-- /tw for last modified file
-- /tc for last created file
local pipe = io.popen(command)
local file_name = pipe:read()
pipe:close()
return file_name
end
local directory = [[C:\path\to\your\directory\]]
local file_name = get_last_file_name(directory)
if file_name then
print(file_name)
-- read the last file
local file = io.open(directory..file_name)
local content = file:read"*a"
file:close()
-- print content of the last file
print(content)
else
print"Directory is empty"
end
【讨论】: