I need to do some housekeeping.I accidentally setup my classpath same as my codebase and all classes are placed along with my code.I need to write a quick java program to select all files of the type .class and .class alone and delete it immediately.Has anyone done something related to this?
+3
A:
Why don't you use the shell to do that, something like:
Linux:
find . -name *.class -print -exec rm {} \;
Windows:
for /r %f in (*.class) do del %f
RC
2010-10-29 05:13:41
make a backup first. Just in case.
Thilo
2010-10-29 05:16:48
I assume that I should run from the package where I should remove the .class files and it will go any levels deep.
Harish
2010-10-29 05:20:51
This will look in sub-directory of the current one. And sure, backup is always a good idea
RC
2010-10-29 05:35:17
Thanks it worked.Voted up.
Harish
2010-10-29 05:45:20
+1
A:
This might work. Untested. Those find/for commands by the others look promising, too, but just in case you are on an OS/390 mainframe here is the Java. ;-)
import java.io.File;
import java.io.IOException;
public class RemoveClass {
public static void main(String[] args) throws Exception {
File f = new File(".");
deleteRecursive(f);
}
public static void deleteRecursive(File f) throws IOException {
if (f.isDirectory()) {
for (File file : f.listFiles()) {
deleteRecursive(file);
}
} else if (f.isFile() && f.getName().endsWith(".class")) {
String path = f.getCanonicalPath();
// f.delete();
System.out.println("Uncomment line above to delete: [" + path + "]");
}
}
}
Julius Davies
2010-10-29 05:20:40
On OS/390 (assuming it's similar to AS/400), there is a `QSHELL` with `find` :)
RC
2010-10-29 05:38:15