Deleting files in a subfolder
Hey all,
I want to create a function that clears out a folder ready for other use. So I've been playing around with a small app. First off I managed to get it to delete files in it's own folder no problem (including itself). However, once I tried to get it to delete files in other folders I hit all sorts of problems. It seems from googling that java has problems with using relative file names (i.e. asking to delete "/Reports/hello.t" won't work) so I started to change the working directory using setProperty(). However it is now just failing to delete the files.
The code I'm using is inside
I want to create a function that clears out a folder ready for other use. So I've been playing around with a small app. First off I managed to get it to delete files in it's own folder no problem (including itself). However, once I tried to get it to delete files in other folders I hit all sorts of problems. It seems from googling that java has problems with using relative file names (i.e. asking to delete "/Reports/hello.t" won't work) so I started to change the working directory using setProperty(). However it is now just failing to delete the files.
The code I'm using is inside
/*
* FileApp.java
*
* Created on 09 May 2004, 16:33
*/
/**
*
* @author Alistair
*/
package Deleter;
import java.io.*;
public class DirectoryApp {
/** Creates a new instance of FileApp */
public DirectoryApp() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Get current directory
String curDir = System.getProperty("user.dir");
System.out.println(curDir);
//Create Pathname for Reports folder
String reportDir = curDir+"\\Reports";
//Change working directory
System.setProperty("user.dir", reportDir);
//display current directory
String newDir = System.getProperty("user.dir");
System.out.println(newDir);
//Print contents of current directory
File dir = new File(newDir);
String[] children = dir.list();
if (children == null) {
//Either dir does not exist or is not a directory
} else {
for (int i=0; i[Error: Irreparable invalid markup ('<children.length;>') in entry. Owner must fix manually. Raw contents below.]
Hey all,
I want to create a function that clears out a folder ready for other use. So I've been playing around with a small app. First off I managed to get it to delete files in it's own folder no problem (including itself). However, once I tried to get it to delete files in other folders I hit all sorts of problems. It seems from googling that java has problems with using relative file names (i.e. asking to delete "/Reports/hello.t" won't work) so I started to change the working directory using setProperty(). However it is now just failing to delete the files.
The code I'm using is inside
<lj-cut>
<pre>
/*
* FileApp.java
*
* Created on 09 May 2004, 16:33
*/
/**
*
* @author Alistair
*/
package Deleter;
import java.io.*;
public class DirectoryApp {
/** Creates a new instance of FileApp */
public DirectoryApp() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Get current directory
String curDir = System.getProperty("user.dir");
System.out.println(curDir);
//Create Pathname for Reports folder
String reportDir = curDir+"\\Reports";
//Change working directory
System.setProperty("user.dir", reportDir);
//display current directory
String newDir = System.getProperty("user.dir");
System.out.println(newDir);
//Print contents of current directory
File dir = new File(newDir);
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[i];
System.out.println(filename);
}
}
//Delete directory contents
if (children == null) {
//no files to delete
} else{
//loop through and delete
for (int i=0; i<children.length; i++ ) {
//delete file
boolean success = (new File(children[i])).delete();
if (!success) {
System.out.println("delete failed");
}
}
}
//display current directory
String new2Dir = System.getProperty("user.dir");
System.out.println(new2Dir);
}
}
</pre>
</lj-cut>
There are lots of System.out.printlns - just to help me try and work out why it isn't working
Any help would be greatfully recieved
Alistair
<b>Edit: All solved now, thanks :)</b>
I want to create a function that clears out a folder ready for other use. So I've been playing around with a small app. First off I managed to get it to delete files in it's own folder no problem (including itself). However, once I tried to get it to delete files in other folders I hit all sorts of problems. It seems from googling that java has problems with using relative file names (i.e. asking to delete "/Reports/hello.t" won't work) so I started to change the working directory using setProperty(). However it is now just failing to delete the files.
The code I'm using is inside
<lj-cut>
<pre>
/*
* FileApp.java
*
* Created on 09 May 2004, 16:33
*/
/**
*
* @author Alistair
*/
package Deleter;
import java.io.*;
public class DirectoryApp {
/** Creates a new instance of FileApp */
public DirectoryApp() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Get current directory
String curDir = System.getProperty("user.dir");
System.out.println(curDir);
//Create Pathname for Reports folder
String reportDir = curDir+"\\Reports";
//Change working directory
System.setProperty("user.dir", reportDir);
//display current directory
String newDir = System.getProperty("user.dir");
System.out.println(newDir);
//Print contents of current directory
File dir = new File(newDir);
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[i];
System.out.println(filename);
}
}
//Delete directory contents
if (children == null) {
//no files to delete
} else{
//loop through and delete
for (int i=0; i<children.length; i++ ) {
//delete file
boolean success = (new File(children[i])).delete();
if (!success) {
System.out.println("delete failed");
}
}
}
//display current directory
String new2Dir = System.getProperty("user.dir");
System.out.println(new2Dir);
}
}
</pre>
</lj-cut>
There are lots of System.out.printlns - just to help me try and work out why it isn't working
Any help would be greatfully recieved
Alistair
<b>Edit: All solved now, thanks :)</b>
