Solved

Deleting files by the date of creation

Posted on 2004-04-28
3
406 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
  • 2
3 Comments
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now