Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find file creation date in Windows using Go

Tags:

windows

go

I was hoping to find the created date of a file in Windows. How would I be able to accomplish this simply?

I have been using os.Stat and os.Chtimes to get other file information but it does not seem to offer information on created date.

like image 519
ev__dev Avatar asked Dec 19 '19 16:12

ev__dev


Video Answer


1 Answers

The FileInfo.Sys method returns the system specific data structures. On Windows, this will be a syscall.Win32FileAttributeData, which looks like

type Win32FileAttributeData struct {
    FileAttributes uint32
    CreationTime   Filetime
    LastAccessTime Filetime
    LastWriteTime  Filetime
    FileSizeHigh   uint32
    FileSizeLow    uint32
}

Getting the creation time would look something like:

d := fi.Sys().(*syscall.Win32FileAttributeData)
cTime = time.Unix(0, d.CreationTime.Nanoseconds())

Since this is windows specific, this of course should be protected by build constraints. Either using a _windows.go file or // +build windows

like image 189
JimB Avatar answered Oct 19 '22 22:10

JimB