Thread: Three ways to get creation date of a file?!

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Three ways to get creation date of a file?!

    So far I have come up with three ways to do the EXACT SAME THING. Namely, get the creation date of an arbitrary file, is there something that I am missing? Is there a difference between these three methods or do they just exist for the sake of redundancy? Is there an advantage to using a particular method?

    METHOD: THE FIRST
    Code:
    string GetFileCreationDate(char* pFileName)
    {
    	stringstream ss;
    	WIN32_FILE_ATTRIBUTE_DATA wfad;
    	SYSTEMTIME st;
    	
    	GetFileAttributesEx(pFileName, GetFileExInfoStandard, &wfad);
    	FileTimeToSystemTime(&wfad.ftCreationTime, &st);
    
    	ss << st.wMonth << '/' << st.wDay << '/' << st.wYear;
    	
    	return ss.str();
    }
    METHOD: THE SECOND
    Code:
    string GetFileCreationDateEx(char* pFileName)
    {
    	stringstream ss;
    	HANDLE hFileHandle = CreateFile(pFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
    	FILETIME ft;
    	SYSTEMTIME st;
    	
    	GetFileTime(hFileHandle, &ft, NULL, NULL);
    	FileTimeToSystemTime(&ft, &st);
    
    	ss << st.wMonth << '/' << st.wDay << '/' << st.wYear;
    	CloseHandle(hFileHandle);
    
    	return ss.str();
    }
    METHOD: THE THIRD
    Code:
    string GetFileCreationDateExEx(char* pFileName)
    {
    	stringstream ss;
    	HANDLE hFileHandle = CreateFile(pFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
    	BY_HANDLE_FILE_INFORMATION bhfi;
    	SYSTEMTIME st;
    
    	GetFileInformationByHandle(hFileHandle, &bhfi);
    	FileTimeToSystemTime(&bhfi.ftCreationTime, &st);
    
    	ss << st.wMonth << '/' << st.wDay << '/' << st.wYear;
    	CloseHandle(hFileHandle);
    
    	return ss.str();
    }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    METHOD: THE FOURTH
    Code:
    string GetFileCreationDateExExEx(char* pFileName)
    {
    	stringstream ss;
    	WIN32_FIND_DATA wfd;
    	SYSTEMTIME st;
    
    	HANDLE hFind = FindFirstFile(pFileName, &wfd);
    	FileTimeToSystemTime(&wfd.ftCreationTime, &st);
    
    	ss << st.wMonth << '/' << st.wDay << '/' << st.wYear;
    	FindClose(hFind);
    
    	return ss.str();
    }
    All these functions serve a different purpose. As to which one to use, GetFileAttributesEx is only available on Win98 and above, GetFileInformationByHandle says "In general, you should not use GetFileInformationByHandle unless your application is intended to be run on a limited set of operating system configurations." and FindFirstFile may be inefficient. Therefore, I would go with GetFileTime if you must support 95 or GetFileAttributesEx if you don't. You can also use the "NewApis.h" header file which will emulate GetFileAttributesEx on Win95 by using FindFirstFile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM