Solved

Deleting files by the date of creation

Posted on 2004-04-28
3
412 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

756 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