0

I'm writing a program/utility in C to find (and then move to a new directory) the files in the current directory that have been modified after the last time the utility was run.

What I'm trying to find out is if there is a way to find the last time this utility ran. Or alternatively, a way to store the time in the program (so as to compare the last stored time against the current time, and then update the "last time" variable to current time).

As I type this it occurs to me that I could write the time to a file (overwriting the single entry as the utility is run) and retrieve the value from the file in the program, although I don't know if this would be the best approach.

countofmontecristo
  • 381
  • 3
  • 5
  • 15
  • 1
    No operating system that I know of stores the last-run time of a program. However, you can easily store the time in a file. – Some programmer dude Oct 31 '14 at 08:26
  • create a file with a specific name in a specific location (so as not to disturb the user's other files). Usually : `/place/where/myprogramis/something/timestamp.file` if you need a global (system wide) timestamp, or `/the/directory/where/I/want/to/monitor/changes/.myprogram_timestamp.file` if you need one per directory the program could be run on (+ add in your program a lookup of everything above the directory as well, so you can find out, if running in some levels below a dir with a timestamp, if you had been run sometimes before ? And on levels below you may update the timestamps as well?) – Olivier Dulac Oct 31 '14 at 08:43
  • Better put the persistent information under `/var/run/` – Basile Starynkevitch Oct 31 '14 at 08:43
  • @BasileStarynkevitch: probably better yes.. The way I'd do it : a single file underneath /var/run (or a more perenne location maybe), that contains a (sorted) list of every directories the program was run in, and the time, (and maybe the user as well, or other informative data you may need, on the same line). Then each time anyone runs the program it needs to check and update that file (so it has to be writable by "everyone"... if you don't care about that file's being tempered with, ok, if you need something better it gets a bit more tricky regarding how to write to the file (+encrypt?)) – Olivier Dulac Oct 31 '14 at 08:48

2 Answers2

1

You can use the last access time from the filesystem (In GNU/linux you can use ls -lu to see last access time).

This is not a portable solution because it depends on filesystem and filesystem settings (see JoachimPileborg edit below)

Moreover look at this question to get last acces time in C (use atime instead of mtime).

Community
  • 1
  • 1
Alepac
  • 1,833
  • 13
  • 24
  • 2
    Last access time is not the same as the last time a program was executed. Some people even turn off the setting of the last access time to speed up file-system operations. – Some programmer dude Oct 31 '14 at 08:35
  • @JoachimPileborg- yes you are right, this is not a portable solution but could be a solution if you know the system when this run (I edited to better explain) – Alepac Oct 31 '14 at 08:39
1

you can make a class contains info and serialize it to a text file , it's more easy to access and can store multiple values, then to store new values first delete file and then create file again.

another approach could be a register key containing information.

hope it would be useful ;)

Ali Esmaeili
  • 542
  • 1
  • 4
  • 14