Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file creation time with lua

Tags:

file

time

lua

How do I get the time that a file was created?

What I have found: The lfs library for Lua contains a method to get file attributes. However, the only ones that seem to get close to answering my question are those:

  • access - time of last access
  • modification - time of last data modification
  • change - time of last file status change

None of them, by their descriptions, check for the creation time specifically. I've been googling for a while and can't find the answer.

EDIT: I'm on a windows system.

like image 800
Xonok Avatar asked Jul 27 '26 17:07

Xonok


2 Answers

Windows command-line provides the argument /T:C to show the file creation date/time when used in context of dir command.

So, you can use the io.popen function as follows:

local sOut = io.popen( "dir /T:C *files*", "r" )
local sData = sOut:read "*a"
-- process sData as string to filter content as your needs
like image 136
hjpotter92 Avatar answered Jul 30 '26 09:07

hjpotter92


At least on Unix, you can't. There are three members related to time in struct stat:

  • st_atime Time of last access.
  • st_mtime Time of last data modification.
  • st_ctime Time of last status change.

Some people misunderstood st_ctime for file creation time, but that's not true. File creation time is not preserved in Unix-style system.

The closest is last status change time, which you could get with lfs library, or, read How can I get last modified timestamp in Lua for a solution without third-part libraries.

like image 25
Yu Hao Avatar answered Jul 30 '26 10:07

Yu Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!