somewhere near the beginning.

Java recursive deletion script

Filed under: Programming — Alex @ 1:56 pm 2/6/2005

This is a java script which deletes all files with a given extension from a directory, including its subdirectories. It’s probably not useful on *Nix platforms, since there are several ways of doing that easily from the command line, but I haven’t found an easy way of doing this kind of thing under Windows.

[java]
import java.lang.*;
import java.io.*;

public class DelAll {

static String ext=”class”; //Delete class files by default

public static void main(String[] args) {

//set the extension to check for
if(args.length>1) {
ext = args[1].toLowerCase();
}

//start in the given directory
deleteAll(new File(args[0]));

}

private static void deleteAll(File curdir) {
File[] fileList = curdir.listFiles();
System.out.println(”in ” + curdir);

for(int i=0; i

// recurse through subdirectories
if(fileList[i].isDirectory()) {
deleteAll(fileList[i]);
}
// remove files with given extension
if(fileList[i].getName().toLowerCase().endsWith("."+ext)) {
System.out.println("deleting " + fileList[i].getName());
fileList[i].delete();
}
}

}
}
[/java]

Possibly relevant posts:

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment