Solved

Deleting files by the date of creation

Posted on 2004-04-28
3
413 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:DimiCamp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10939517
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10939548
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 250 total points
ID: 10939733
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question