views:

32

answers:

3

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
make a backup first. Just in case.
Thilo
I assume that I should run from the package where I should remove the .class files and it will go any levels deep.
Harish
This will look in sub-directory of the current one. And sure, backup is always a good idea
RC
Thanks it worked.Voted up.
Harish
+1  A: 
find . -name "*.class" -exec rm '{}' \;
Sam Barnum
+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
On OS/390 (assuming it's similar to AS/400), there is a `QSHELL` with `find` :)
RC