Link to home
Start Free TrialLog in
Avatar of Cpp_01
Cpp_01

asked on

how to get the time stamp of a file or folder?

In C++,how to get the time stamp of the File?That is how to know if the file has been modified with compare to old file.
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

man stat
 or
man fstat
What OS?

For Windows, you can use the following C Run Time library functions.

_fstat, _fstati64
_stat, _wstat, _stati64, _wstati64

Or, the following API functions.

GetFileTime
GetFileInformationByHandle
Avatar of corduroy9
corduroy9


If you are using MFC...

      CString sTifFileDateTime;
      CFileStatus status;

      try
      {
            CFile cfile( sFile, CFile::modeRead );  //PUT YOUR FILE NAME HERE
            if( cfile.GetStatus( status ) )    
            {

                  //get the file's date/time and format it
                  CString sFileDateTime;
                  CTime fileTime = status.m_mtime;
                  sFileDateTime = fileTime.Format( "%Y%m%d%H%M%S0000" );
            }
            else
            {
                  sError = "Could not get file statistics on file " + sFile;
                  cout << sError << endl;
                  return -1;
            }
            cfile.Close();
      }
      catch (CFileException *ex )
      {
            CString sCause;
            switch ( ex->m_cause )
            {
                  case CFileException::generic:
                        sCause = "An unspecified error occurred.";
                        break;
                  case CFileException::fileNotFound:
                        sCause = "The file could not be located.";
                        break;
                 ....
                                }
                 }


ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial