Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting files

Status
Not open for further replies.

smartglass

IS-IT--Management
Mar 14, 2006
33
0
0
GB
Hi:
I am trying to write a little utility that deletes all pdf files in a given directory over 14 days old.
remove() can delete specified files; but does anybody know how to delete a range, and over a given age?
Thanks!
 
You will need to look at FindFirst() and FindNext(). Look at thread101-525513 for starters.

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
tri this


/*
Gary Russell
ghrussel@swbell.net
ISSW
Deletes all files in the current directory over 14 days old.
*/
#include <conio.h>
#include <io.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <dir.h>

//---------------------------------------------------------------------------
//Get the day of the year.
int get_yday(char s, int day, int mo) {
int add, yday;

if(mo == 1) { add = 0; }
if(mo == 2) { add = 31; }
if(mo == 3) { add = 59; }
if(mo == 4) { add = 90; }
if(mo == 5) { add = 120; }
if(mo == 6) { add = 151; }
if(mo == 7) { add = 181; }
if(mo == 8) { add = 212; }
if(mo == 9) { add = 243; }
if(mo == 10) { add = 273; }
if(mo == 11) { add = 304; }
if(mo == 12) { add = 334; }

if(s != 'f' && mo == 12 && day == 17) { }

yday = add + day;

return yday;
}

//---------------------------------------------------------------------------
int main() {
int fday, sday, done;
char type, end[] = "*.*";
FILE *fp;
std::ftime ft;
struct date d;
struct ffblk ffblk;

done = findfirst(end, &ffblk, 0);

while (!done) {

if((fp = fopen(ffblk.ff_name, "rb")) == NULL) {
printf("\n\nCan not open output file!\n"); getch(); exit(1); }

getdate(&d);

type = 's';
sday = get_yday(type, d.da_day, d.da_mon);

getftime(fileno(fp), &ft);

fclose(fp);

type = 'f';
fday = get_yday(type, ft.ft_day, ft.ft_month);

if(sday + 13 < fday) { remove(ffblk.ff_name); }

done = findnext(&ffblk);
}

return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top