Lua 获取文件最后修改时间

寻找一种在 Lua 中获取文件最后修改时间的方法,在 windows 上

我看过这篇文章How can I get last modified timestamp in Lua 但是这是针对 Linux 的解决方案,我能在 windows 中使用 io.popen 获取字符串吗?

点赞
用户13447666
用户13447666

通过使用 luafilesystem git https://github.com/keplerproject/luafilesystem 中的 lfs.attributes(<你的文件路径>).modification 方法,你可以找到你正在寻找的内容。

它同样适用于 Windows 和 Linux。

手册 https://keplerproject.github.io/luafilesystem/manual.html

2020-12-06 04:19:40
用户1847592
用户1847592
function get_file_time(filepath)
  local pipe = io.popen('dir /4/tw "'..filepath..'"')
  local output = pipe:read"*a"
  pipe:close()
  return output:match"\n(%d.-:%S*)"
end

local filepath = [[C:\path\to\your\file.ext]]  -- file must exist
print(get_file_time(filepath))

将获取文件时间戳的函数翻译成中文并保留 markdown 格式:

function get_file_time(filepath)
  -- 执行命令获取文件时间戳
  local pipe = io.popen('dir /4/tw "'..filepath..'"')
  local output = pipe:read"*a"
  pipe:close()
  -- 提取时间戳
  return output:match"\n(%d.-:%S*)"
end

local filepath = [[C:\path\to\your\file.ext]]  -- 文件必须存在
print(get_file_time(filepath))
2020-12-06 05:07:26