【问题标题】:How to copy latest file from a directory using lua如何使用lua从目录中复制最新文件
【发布时间】:2020-02-07 04:04:40
【问题描述】:

我正在尝试使用 lua 文件从目录中仅复制最新文件。 最新文件意味着:取决于修改时间/创建时间。 我该怎么做?

【问题讨论】:

  • 你的操作系统是什么?
  • 我会推荐你​​使用LuaFileSystem

标签: lua last-modified


【解决方案1】:

参考这个问题: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 函数来进行复制。

【讨论】:

  • 我正在尝试在 windows 环境中获取我的结果。并且输出总是在当前时间到来。无法获取最新的文件时间。
【解决方案2】:
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

【讨论】:

  • 非常感谢我能够检查最新的文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
相关资源
最近更新 更多