• C

Deleting files by the date of creation

I’ve made an application that generates some.txt files every day. I need a history by keeping the last 3 days files. How can I delete the older ones periodically?
DimiCampAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ankuratvbConnect With a Mentor Commented:
Here's an example of stat(),and difftime().
For multiple files,you could walk through the directory(as sjef_bosman pointed out),using findfirst(),findnext()(or readdir() on unix).

#include <sys\stat.h>
#include <stdio.h>
#include <time.h>

#define FILENAME "TEST.TXT"

int main(void)
{
   struct stat statbuf;
   FILE *stream;

   /* open a file for update */
   if ((stream = fopen(FILENAME, "w+")) == NULL)
   {
      fprintf(stderr, "Cannot open output file.\n");
      return(1);
   }

   /* get information about the file */
   stat(FILENAME, &statbuf);

   fclose(stream);

   printf("Time file : %s\n", ctime(&statbuf.st_ctime));
   time_t now;
   now=time(NULL);
   printf("The difference is: %f seconds\n",difftime(now,statbuf.st_ctime));
   return 0;
}

0
 
ankuratvbCommented:
check the date creation property using stat(),use difftime() to evaluate the interbal b/w that time and now,and then delete it accordingly using remove()
0
 
Sjef BosmanGroupware ConsultantCommented:
Assuming you have to write a program to do this, you could just walk through a directory and ask every file's properties. Then delete those you don't need. What platform do you use?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.