Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting files by the date of creation

Posted on 2004-04-28
3
Medium Priority
?
416 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

730 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