Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the time that a file was last written to

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
I am trying to find the time that a particular file has been written to last. The following is the code I used:<br><br>void CNodeLockIDGenDlg::OnButton1() <br>{<br> <br> OFSTRUCT ofs;<br>HFILE hFile;<br>if(hFile=OpenFile(&quot;c:\\temp.cor&quot;, (LPOFSTRUCT) &ofs, OF_READ) == HFILE_ERROR) //temp.cor is just a file I created today<br>AfxMessageBox(&quot;Could not open file!!&quot;);<br><br><br>FILETIME lpLastWriteTime;<br>//FILETIME lpCreateTime;<br>//FILETIME lpAccessTime;<br><br><br>int a=GetFileTime(<br> (HANDLE)hFile,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle to file<br> (LPFILETIME) NULL,&nbsp;&nbsp;&nbsp;&nbsp;// creation time<br> (LPFILETIME) NULL,&nbsp;&nbsp;// last access time<br> &lpLastWriteTime&nbsp;&nbsp;&nbsp;&nbsp;// last write time<br> );<br>SYSTEMTIME lpSysTime;<br>if(!FileTimeToSystemTime(<br> &lpLastWriteTime,&nbsp;&nbsp;// file time to convert<br> &lpSysTime&nbsp;&nbsp;&nbsp;&nbsp;// receives system time<br> ))<br>AfxMessageBox(&quot;Something wrong&quot;);<br><br>m_Temp=lpSysTime.wYear; //m_temp is a edit box of type DWORD<br>UpdateData(false);<br>CloseHandle((HANDLE) hFile);<br><br>}<br><br>The result I keep getting back is 1618 this happens even if I change the file that hFile is pointing to. Does anyone have any ideas on what I am doing wrong?<br><br><br>Cory<br><br><br><br>
 
I have figured out where my error is but not how to fix it.<br>I used getLastError() on:<br><br>&nbsp;&quot;int a=GetFileTime(<br>(HANDLE)hFile,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle to file<br>(LPFILETIME) NULL,&nbsp;&nbsp;&nbsp;&nbsp;// creation time<br>(LPFILETIME) NULL,&nbsp;&nbsp;// last access time<br>&lpLastWriteTime&nbsp;&nbsp;&nbsp;&nbsp;// last write time<br>);&quot;<br><br>It told me that the handle hFile was invalid. Does anyone know why?<br><br>
 
OOH I got it. I was being stupid the line:<br>if(hFile=OpenFile(&quot;c:\\temp.cor&quot;, (LPOFSTRUCT) &ofs, OF_READ) == HFILE_ERROR)<br>should be:<br>if((hFile=OpenFile(&quot;c:\\temp.cor&quot;, (LPOFSTRUCT) &ofs, OF_READ)) == HFILE_ERROR)<br><br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top