How to check a file is modified or not

5 03 2009

description2

 

 

Sometimes we need to closely observe a file in which its modified or not. The only way  to do this is to check the last modified time of the file in particular intervals.

howcanidoit2

 

 

MFC is providing a useful structure CFileStatus for that. We could take this structure of our file in particular time intervals (say 500ms) and compare the new value with the previously taken value. Actually  the CFile::GetStatus function retrieves the status information related to the given CFile object or a given file path.

The CFileStatus structure has the following fields:

CTime m_ctime: The date and time the file was created.

CTime m_mtime: The date and time the file was last modified.

CTime m_atime: The date and time the file was last accessed for reading.

ULONGLONG m_size: The logical size of the file in bytes, as reported by the DIR command.

BYTE m_attribute: The attribute byte of the file.

char m_szFullName[_MAX_PATH]:  The absolute filename in the Windows character set.

mycodesnippet2

 

 

CTime m_prevTime;
//we could execute this function in a loop with 500ms delay
void IsFileModified()
{
   CFile cfile;
   cfile.Open(_T("D:\\SAN\\sample.txt"), CFile::modeRead);
  
   CFileStatus status;
  
   if( cfile.GetStatus(status) )
   {
      if( m_prevTime < status.m_mtime )
      {
         AfxMessageBox("File is modified");
      }
      m_prevTime = status.m_mtime;
   }
}

mynote3

 

 

The static version of GetStatus obtains the file status from a given file path without actually opening the file.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: