systematic file(s) deletion

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.
 
D

Daniel Dyer

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"; ??
*/
...

I'd use the find command (under Cygwin on Windows). The command would
look something like this (I have tested to make sure this is correct):

find "C:\Program Files\ThisProgram\DataFiles" -mtime 3 -exec rm -f {} \;

Dan.
 
D

Daniel Dyer

I'd use the find command (under Cygwin on Windows). The command would
look something like this (I have tested to make sure this is correct):

Sorry, I meant *haven't* tested (as in don't blame me if it trashes your
file system).

Dan.
 
N

NickName

Daniel said:
Sorry, I meant *haven't* tested (as in don't blame me if it trashes your
file system).

Dan.

Ok, I just looked up info on Cygwin. It would not be applicable to
what I need in my working environment. Additionally, I'd like to use
this case to learn more basics on file manipulation in java. Thanks
though.
 
O

Oliver Wong

NickName said:
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"; ??
*/

The concept of a "current" directory isn't well defined in Java,
possibly because it isn't well defined across all the OSes that Java
supports. If you wanted to make this an actual utility for users to use,
best to have the user provide the path to the directory they want to operate
on as a command line argument or something similar.
...

// 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?


No, you can't use "dynamic var names" in the way that you described. The
closest thing I can think of to what you're trying to do is to use an array.
But a simpler way would be to not keep around a reference to every file
you're going to work with, but rather to just keep a reference to the single
file you're currently working with at that time.
File "file"&c = new File(filename);
long "file"&c&"date = "file"&c.lastModified;

File doesn't have a publicly accessible "lastModified" field, but it has
a lastModified() method which you can invoke.
/* now compare each file's date stamp against today's date if
it's two days old delete it,
DatePart? How to? */

Numerical comparison is typically done via the >, >=, ==, !=, =< and <
operators.
...
// delete file
boolean del = (new File("file"&c)).delete();

}
}

Better way? Many thanks in advance.

This is probably a dangerous program to learn Java with, since you might
mistakenly delete data that you don't want to delete by accidentally
introducing bugs into your program. Why not write a less destructive program
for the purposes of learning? Maybe one that merely *lists* all files older
than a certain age, and leaves it up to the user to manually delete them?

- Oliver
 
N

NickName

Thanks for the very helpful advice, pls see below.

Oliver said:
news:[email protected]... [...]

The concept of a "current" directory isn't well defined in Java,
possibly because it isn't well defined across all the OSes that Java
supports. If you wanted to make this an actual utility for users to use,
best to have the user provide the path to the directory they want to operate
on as a command line argument or something similar.

Good idea of letting user to provide this via a param, however, the
downside, could be a typo in the path, so, maybe let user select a
directory would be good.
No, you can't use "dynamic var names" in the way that you described. The
closest thing I can think of to what you're trying to do is to use an array.
But a simpler way would be to not keep around a reference to every file
you're going to work with, but rather to just keep a reference to the single
file you're currently working with at that time.

Good to know.
File doesn't have a publicly accessible "lastModified" field, but it has
a lastModified() method which you can invoke.

My typo, it should have read "file"&c.lastModified[];
Numerical comparison is typically done via the >, >=, ==, !=, =< and <
operators.

Good to know. A specific date comparison question below.

// list all files under Temp folder.
File dir = new File("Temp");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a
directory
} else {
for (int i = 0; i < children.length; i++) {
// Get filename of file or directory
String filename = children;

if ((new Date()) - (new
Date(filename.???()))) > 30
// find file(s) that is older than 30
days
// what method to use?

{
System.out.println("\nTemp: " +
filename);
}
}
}

This is probably a dangerous program to learn Java with, since you might
mistakenly delete data that you don't want to delete by accidentally
introducing bugs into your program. Why not write a less destructive program
for the purposes of learning? Maybe one that merely *lists* all files older
than a certain age, and leaves it up to the user to manually delete them?

Well, I could use a TEMP directory, data there are no longer of value.
 
J

John Ersatznom

NickName said:
Thanks for the very helpful advice, pls see below.

Oliver said:
[...]

The concept of a "current" directory isn't well defined in Java,
possibly because it isn't well defined across all the OSes that Java
supports. If you wanted to make this an actual utility for users to use,
best to have the user provide the path to the directory they want to operate
on as a command line argument or something similar.

Good idea of letting user to provide this via a param, however, the
downside, could be a typo in the path, so, maybe let user select a
directory would be good.

There's something called JFileChooser that you might find interesting if
so. :)
 
N

NickName

John said:
NickName said:
Thanks for the very helpful advice, pls see below.

Oliver said:
[...]

The concept of a "current" directory isn't well defined in Java,
possibly because it isn't well defined across all the OSes that Java
supports. If you wanted to make this an actual utility for users to use,
best to have the user provide the path to the directory they want to operate
on as a command line argument or something similar.

Good idea of letting user to provide this via a param, however, the
downside, could be a typo in the path, so, maybe let user select a
directory would be good.

There's something called JFileChooser that you might find interesting if
so. :)

Good to know. Thanks. Could you answer the following question?

A specific date comparison question below.

// list all files under Temp folder.
File dir = new File("Temp");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a
directory
} else {
for (int i = 0; i < children.length; i++) {
// Get filename of file or directory
String filename = children;


if ((new Date()) - (new Date(filename.???()))) > 30
// find file(s) that is older than 30 days
// what method to use?


{
System.out.println("\nTemp: " + filename);
}
}
}
 
O

Oliver Wong

NickName said:
String filename = children;

if ((new Date()) - (new
Date(filename.???()))) > 30
// find file(s) that is older than 30
days
// what method to use?


The File class has a method to get the date of last modification, called
lastModified(). You have an instance of a String, which presumably
represents the path to a file. File also has a constructor which accepts a
String.

- Oliver
 
N

NickName

Oliver said:
NickName said:
String filename = children;

if ((new Date()) - (new
Date(filename.???()))) > 30
// find file(s) that is older than 30
days
// what method to use?


The File class has a method to get the date of last modification, called
lastModified(). You have an instance of a String, which presumably
represents the path to a file. File also has a constructor which accepts a
String.

- Oliver


Right. Initially I used the lastModified() method, the code looked
like,
if ((new Date()) - (new Date(filename.lastModified())) > 30
// find file(s) that is older than 30 days
// assumed the millseconds and whatever Date() produces
would
// be able to auto convert?
But it failed to compile. Or am I wrong?

Many thanks.
 
O

Oliver Wong

NickName said:
Right. Initially I used the lastModified() method, the code looked
like,
if ((new Date()) - (new Date(filename.lastModified())) > 30
// find file(s) that is older than 30 days
// assumed the millseconds and whatever Date() produces
would
// be able to auto convert?
But it failed to compile. Or am I wrong?

Invoke lastModified() on the date, not the string.

new Date(filename).lastModified()

- Oliver
 
N

NickName

Oliver said:
Invoke lastModified() on the date, not the string.

new Date(filename).lastModified()

- Oliver

Thanks. Made change accordingly, however, the following code still
won't work
if ((new Date()) - (new Date(filename).lastModified())) > 30 {
System.out.println("\nTemp files older than 30 days: " +
filename);
};

Also, tried,
System.out.println(new Date(filename).getTime());
for testing, won't work neither. What's wrong?
 
O

Oliver Wong

NickName said:
Thanks. Made change accordingly, however, the following code still
won't work
if ((new Date()) - (new Date(filename).lastModified())) > 30 {
System.out.println("\nTemp files older than 30 days: " +
filename);
};

Also, tried,
System.out.println(new Date(filename).getTime());
for testing, won't work neither. What's wrong?

In the future, when something "won't work", you should probably state
what results you got (your computer exploded? You got a compile error? A
runtime exception? The program runs fine, but didn't do what you expected it
to do?), what results you expected (the program is supposed to calculate the
fibbonacci sequence? Determine prime factors?), and how they differed (in
case the difference is too subtle for us to notice).

I'm guessing that you had some sort of compile error in the first case,
and I have no idea what results you got with the second case (maybe a
ParseException? Or it ran but with results you didn't understand nor
expect?)

The problem in the first case is that you're applying the subtraction
operator on a Date object, "(new Date()) - [something]" when the subtraction
operator is only defined on numbers. Based on the text in your println()
statement, it looks like you're testing whether a file is older than 30
days.

If that's the case, notice that the string that you supply to the Date
constructor is supposed to represent a date, and not a filename. E.g. the
contents of the string should look like "Jan 1st, 2007" and not
"C:\myfile.txt".

- Oliver
 
N

NickName

Oliver said:
NickName said:
In the future, when something "won't work", you should probably state
what results you got (your computer exploded? You got a compile error? A
runtime exception? The program runs fine, but didn't do what you expected it
to do?), what results you expected (the program is supposed to calculate the
fibbonacci sequence? Determine prime factors?), and how they differed (in
case the difference is too subtle for us to notice).

I'm guessing that you had some sort of compile error in the first case,
and I have no idea what results you got with the second case (maybe a
ParseException? Or it ran but with results you didn't understand nor
expect?)

The problem in the first case is that you're applying the subtraction
operator on a Date object, "(new Date()) - [something]" when the subtraction
operator is only defined on numbers. Based on the text in your println()
statement, it looks like you're testing whether a file is older than 30
days.

If that's the case, notice that the string that you supply to the Date
constructor is supposed to represent a date, and not a filename. E.g. the
contents of the string should look like "Jan 1st, 2007" and not
"C:\myfile.txt".

- Oliver

Ok. Sorry for not providing exact err msg.

Now, here's the latest attempt. Many thanks.

// test output today's day extraction, divide milliseconds
System.out.println("test date manipulation:" + (new
Date().getTime()) / (8640000));
// result: successful


// list files

File dir = new File("Temp");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a
directory
} else {
for (int i = 0; i < children.length; i++) {
// Get filename of file or directory
String filename = children;
File fd = new File(filename);
long ftime = fd.lastModified();

// test output of the file day value
System.out.println(ftime);
// result: 0
// comment: bad
// question: what's wrong?

// System.out.println("now = " + new
Date().getTime());
/* debug */
// 1000 * 60 * 60 * 24 = 1 day = 8640000

// System.out.println((new File(filename).lastModified()) /
(8640000));
/*
if ( (new Date().getTime()) / (8640000) - (new
File(filename).lastModified()) / (8640000) ) > 30
{
System.out.println("\nTemp files
older than 30 days: " + filename);
};
*/
}
 
O

Oliver Wong

NickName said:
Oliver said:
In the future, when something "won't work", you should probably state
what results you got (your computer exploded? You got a compile error? A
runtime exception? The program runs fine, but didn't do what you expected
it
to do?), what results you expected (the program is supposed to calculate
the
fibbonacci sequence? Determine prime factors?), and how they differed (in
[...]

Now, here's the latest attempt.
[code snipped]

Okay. Does it work and you're just showing it to us, or is there some
problem and you want us to help you fix the problem?

- Oliver
 
E

EJP

Oliver said:
Invoke lastModified() on the date, not the string.

new Date(filename).lastModified()

Nonsense. Date doesn't have a lastModified() method. What you need is

if ((System.getCurrentTimeMillis() - (filename.lastModified()) >
30*24*60*60*1000L)
 
O

Oliver Wong

EJP said:
Nonsense. Date doesn't have a lastModified() method. What you need is

if ((System.getCurrentTimeMillis() - (filename.lastModified()) >
30*24*60*60*1000L)

You're right about the date not having a lastModified method, but
neither does filename, which I suspect is a String.

In other words, the OP really needs to address the usenet issues I've
been addressing before we can hope to seriously help with the problem: Post
an SSCCE, state exactly what the problem is (it won't compile? An exception
is thrown? Something else?), and state what the program is supposed to do.

Without this information, everyone will be confused (as demonstrated by
my error, and EJP's error, etc.) and will give incorrect advice.

I've lost the motivation to help NickName, and probably will not regain
this motivation until the SSCCE and other issues above are addressed.

- Oliver
 
N

NickName

Oliver said:
news:[email protected]... [...]
In other words, the OP really needs to address the usenet issues I've
been addressing before we can hope to seriously help with the problem: Post
an SSCCE, state exactly what the problem is (it won't compile? An exception
is thrown? Something else?), and state what the program is supposed to do.

Without this information, everyone will be confused (as demonstrated by
my error, and EJP's error, etc.) and will give incorrect advice.

I've lost the motivation to help NickName, and probably will not regain
this motivation until the SSCCE and other issues above are addressed.

- Oliver


/* objective: list files in the Temp directory that are 30 days older
than today's date */

/* one way to solve the problem: compare today's date with each file's
dateTimeStamp */

// specifically, the technique to use here is to get today's date value
by
// using (new Date().getTime()) / (8640000));

// and another technique of using File Class's lastModified() method
// to get "similar" value for the date of each file
// this technique works with "known" file, see a case below.

File fileD = new File("TestFile.txt");
long fdate = fileD.lastModified();
System.out.println("\t and it was last edited on " + fdate);

// here's the clean code with Comments

// list files
// the Temp directory has 10 files, of which one
file name includes white spaces

File dir = new File("Temp");


String[] children = dir.list();
if (children == null) {

} else {
for (int i = 0; i < children.length; i++) {

String filename = children;
File fd = new File(filename);

// debug: to print out each file name
System.out.println(fd);
// result: yes it does

long ftime = fd.lastModified();
// test output of the file day value
System.out.println(ftime);
// result: 0
// comment: bad
// question: what's wrong?

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top