How to Get Last Accessed/Created/Modified File Date and Time

Reply

Join Date: May 2005
Posts: 13
Reputation: skprasat is an unknown quantity at this point 
Solved Threads: 0
skprasat skprasat is offline Offline
Newbie Poster

How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #1
Jun 15th, 2005
Hi,

Does anyone know how to check for a file access date and time? The function returns the modified date and time and i need something that compares the accessed date time to the current date and time.

regards
prasad....
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #2
Jun 15th, 2005
If you are programming for windows you can use the GetFileTime( ) API to retrieve the Created/LastAccessed/and Last Modified times. These are output in the FILETIME structure, so you will have to use the FileTimeToSystemTime() in order to change the FILETIME structure to a SYSTEMTIME structure.
After that , you can get the current systemtime using the GetSystemTime() and compare this with the times you got for the file.

You can find more in

http://msdn.microsoft.com/library/de..._functions.asp
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: skprasat is an unknown quantity at this point 
Solved Threads: 0
skprasat skprasat is offline Offline
Newbie Poster

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #3
Jun 15th, 2005
Thank you so much for ur response.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: skprasat is an unknown quantity at this point 
Solved Threads: 0
skprasat skprasat is offline Offline
Newbie Poster

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #4
Jun 15th, 2005
im still confused of using this....the time and date is not displyg properly,
can any1 explin me how to print the date and time..
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #5
Jun 16th, 2005
Have you been able to get the dates and times? The thing is that SYSTEMDATE is not in the usual "yy mm dd" format. To display this in the way we all understand, you will have to use GetDateFormat and GetTime Format functions. Better is you give us the code up to where you have got the file times and system times. Then it is easier to narrow out the problem.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: skprasat is an unknown quantity at this point 
Solved Threads: 0
skprasat skprasat is offline Offline
Newbie Poster

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #6
Jun 16th, 2005
look at this code..its working, but not displyg the time properly.

im getg the output like..
last modified date and time = 15-06-05 00:00:00
but im get the current time and date correctly.

currrent time =3:11:44
current date =16/05/05

the next thing what im trying is, to compare both the last modified time and date to the current date and time. can u explain me how to do that...

my code is:

char filename[] = "c:\\test.txt";
if (!stat(filename, &buf))
{
struct tm* atime = localtime(&buf.st_atime);

strftime(timeStr, 100, "%d-%m-%Y %H:%M:%S", atime);


printf("\nLast modified date and time = %s\n", timeStr);
}
else
printf("error getting atime\n");
_strtime(time);
_strdate(date);
printf("\nThe Current time is %s\n",time);
printf("\nThe Current Date is %s\n",date);

thanx in advance,

regards
prasad
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #7
Jun 16th, 2005
  1. #include <time.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <stdio.h>
  5.  
  6. int main()
  7. {
  8. char filename[] = "c:\\test.txt";
  9. char timeStr[ 100 ] = "";
  10. struct stat buf;
  11. time_t ltime;
  12. char datebuf [9];
  13. char timebuf [9];
  14.  
  15. if (!stat(filename, &buf))
  16. {
  17. strftime(timeStr, 100, "%d-%m-%Y %H:%M:%S", localtime( &buf.st_mtime));
  18. printf("\nLast modified date and time = %s\n", timeStr);
  19. }
  20. else
  21. {
  22. printf("error getting atime\n");
  23. }
  24. _strtime(timebuf);
  25. _strdate(datebuf);
  26. printf("\nThe Current time is %s\n",timebuf);
  27. printf("\nThe Current Date is %s\n",datebuf);
  28. time( &ltime );
  29. printf("\nThe Current time is %s\n",ctime( &ltime ));
  30. printf("\Diff between current and last modified time ( in seconds ) %f\n", difftime(ltime ,buf.st_mtime ) );
  31. return 0;
  32. }

My Output was
Last modified date and time = 06-04-2005 17:24:12

The Current time is 10:25:53

The Current Date is 06/17/05

The Current time is Fri Jun 17 10:25:53 2005

Diff between current and last modified time ( in seconds ) 6195701.000000
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: skprasat is an unknown quantity at this point 
Solved Threads: 0
skprasat skprasat is offline Offline
Newbie Poster

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #8
Jun 17th, 2005
Thanku so much...

From ur code i can find only the last modified date and time..
ie only i changd something in that file will be reflected.

what i need is the last accessed time...
can u help me for that..

thanks in advance

regards
prasad
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #9
Jun 17th, 2005
Use buf.st_atime instead of buf.st_mtime. That is the last accessed time. But keep in mind that since you are accessing that file in your program, the result will be the time you run the program. Even if you right click the file in Windows ( at least in my Windows 2000 OS ) and select properties, you will see that the last accessed time is the time you right clicked the file. I dont know anyway of getting around that. I personally think that the last modified time ( buf.st_mtime) is the one that matters the most in your application. But it can be otherwise. Good luck.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Get Last Accessed/Created/Modified File Date and Time

 
0
  #10
Jun 17th, 2005
I think I figured out the problem. It is the File System which makes the stat::a_time come out the same. I found the below in the documentation.

st_atime
Time of last access of file. Valid on NTFS but not on FAT formatted disk drives. Gives the same
Try running this in an NTFS partition, and check the results. Hopefully there will be an improvement in results.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC