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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using to c to cleanup files

Status
Not open for further replies.

crymedry

MIS
Nov 19, 2003
54
what command in c can i use to check the modify date of files in different directories and delete the ones older than 30 days?

thanks for you help
 
Start with this thread thread205-622190 apply a check of the current date with the file date, and if its older by 30 days, print it

When you're sure its printing correctly, change it to delete

--
 
that is one of the problems, i don't know what command compares the modified date, and i only need it to do one directory on the machine, it is a temp folder that we are starting to delete all the older items out of automatically.
 
difftime() is pretty good

Though there may be something more appropriate in a win32 environment (which are you using?).


--
 
i ahven't decided yet, i'm trying to establish what enviroment would be the best to do this in ..... ie: c, vb, or standard batch file
 
Well if you don't mind a bit of manual work, you could just use windows explorer file find, click on the more options and pick a date range which ends 30 days ago.
Then do select all (ctrl-A probably) and press delete


--
 
yes, i could, but we are trying to automate the process.

we are starting it at deleting items that are 30 days old and are gradually going to move it up to where all the items in the folder are being deleted nightly.

that is why i am trying to write a program/script for it.
i was told that i could xcopy items older than 30 days to a new location and delete them from there, but i don't know any options that i can use in a batch file to specify the date modified to be a certain range.

if you do, please tell me, i would love to just write a bach file for this and do it that way

thanks
 
Here's the source of an old DOS utility that I used to use for this purpose. It's not super elegant & I don't have time to go into the details right now, but it should give you an idea of one way to go about this if you're wanting to do it in C.

Code:
#include <stdio.h>
#include <dir.h>
#include <time.h>

#define SECSINDAY 51000

int main(void) {

   struct ffblk ffblk;
   int done;
   int pastdate;
   time_t timer;
   struct tm *tblock;
   int days = 30;  

   timer = time(NULL) - (days * SECSINDAY);  

   tblock = localtime(&timer);

   // shift values into pastdate to give same format as ffblk.ff_fdate
   pastdate = ((tblock->tm_year - 80) << 9)
	      + ((tblock->tm_mon+1) << 5)
	      + tblock->tm_mday;

   done = findfirst(&quot;*.*&quot;,&ffblk,0);
   while (!done) {
     printf(&quot;%s - &quot;,ffblk.ff_name);
     if (ffblk.ff_fdate <= pastdate) {
       printf(&quot;delete\n&quot;);
       remove(ffblk.ff_fname);
     }
     else printf(&quot;keep\n&quot;);
     done = findnext(&ffblk);
   }

   return 0;
}

Good Luck.
 
You could do it with a cscript/jscript.
or with cygwin use the find command, piping the result to a delete/erease.

one of the find parms will give you files based on date accessed, file size etc.
 
where can i find some info on that scripting, that sounds like what i want to do, right now i am looking into vbscript to do it, but am having an issue with finding files without specifying an exact file name.

thanks
 
Go to search for cscript. You may have to download and install the software. But it is similar to vb without having VB on a machine to run.




Script Host Options
Both CScript and WScript accept a number of options that either affect how the script host will run a script or modify some aspect of the WSH environment. The two script hosts share a common set of options; CScript also has a few options, most notably //logo and //nologo, which have no effect in WScript.

When WSH is first installed, WScript is configured as the default script host. (If you do not specify either CScript or WScript when starting a script, WSH runs scripts using the default script host.) To set the default script host to CScript, type the following at the command prompt:

cscript //H:cscript

To reset WScript as the default script host, type this:

wscript //H:wscript

Table 3.2 lists a number of the more commonly used WSH options.
 
In addition, the script has an object which provides all the info that you will need to parse and delete the files.

The web site has docs which show you how to use it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top