N
NickName
Hi,
Say, I have a bunch of files sitting under C:\Program
Files\ThisProgram\DataFiles directory,
and I'm running out of disk space, so, I'd like to sysmatically remove
all the file that is more than two days old under this directory. How
would I do that?
Please work with me through the following semi-code.
// first, need to point to/move to this directory, how?
/*
There's this method to find out current directory,
System.getProperty("user.dir");
is there any method that would set current directory such as
System.setProperty("user.dir") = "C:\Program
Files\ThisProgram\DataFiles"; ??
*/
....
// now, we're ready to instantiate the directory
File dir = new File("MyDirectoryName");
// use var children as file names, use array to store them
String[] children = dir.list();
// validation
if (children == null) {
// Either dir does not exist or is not a directory
} else {
// some sort of preparation
int c = 0;
Date today = new Date();
// iteration of files
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children;
// debug to display files
System.out.println(filename);
// find each file's date/time stamp
// var c for file counter, increment it
c++;
// could we use dynamic var name like "file"&c where "file" is
string and c is var value?
File "file"&c = new File(filename);
long "file"&c&"date = "file"&c.lastModified;
/* now compare each file's date stamp against today's date if
it's two days old delete it,
DatePart? How to? */
...
// delete file
boolean del = (new File("file"&c)).delete();
}
}
Better way? Many thanks in advance.
Say, I have a bunch of files sitting under C:\Program
Files\ThisProgram\DataFiles directory,
and I'm running out of disk space, so, I'd like to sysmatically remove
all the file that is more than two days old under this directory. How
would I do that?
Please work with me through the following semi-code.
// first, need to point to/move to this directory, how?
/*
There's this method to find out current directory,
System.getProperty("user.dir");
is there any method that would set current directory such as
System.setProperty("user.dir") = "C:\Program
Files\ThisProgram\DataFiles"; ??
*/
....
// now, we're ready to instantiate the directory
File dir = new File("MyDirectoryName");
// use var children as file names, use array to store them
String[] children = dir.list();
// validation
if (children == null) {
// Either dir does not exist or is not a directory
} else {
// some sort of preparation
int c = 0;
Date today = new Date();
// iteration of files
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children;
// debug to display files
System.out.println(filename);
// find each file's date/time stamp
// var c for file counter, increment it
c++;
// could we use dynamic var name like "file"&c where "file" is
string and c is var value?
File "file"&c = new File(filename);
long "file"&c&"date = "file"&c.lastModified;
/* now compare each file's date stamp against today's date if
it's two days old delete it,
DatePart? How to? */
...
// delete file
boolean del = (new File("file"&c)).delete();
}
}
Better way? Many thanks in advance.