user1869316
user1869316

Reputation:

Delete all files with an extension using Java

I'm (relatively) new to Java and I'm trying to implement a .jar that runs a list of commands that in Windows XP's command prompt it would be:

cd\
cd myfolder
del *.lck /s

My (failed) attempt:

// Lists all files in folder
File folder = new File(dir);
File fList[] = folder.listFiles();
// Searchs .lck
for (int i = 0; i < fList.length; i++) {
    String pes = fList.get(i);
    if (pes.contains(".lck") == true) {
        // and deletes
        boolean success = (new File(fList.get(i)).delete());
    }
}

I screwed somewhere around that "get(i)", but I think I'm pretty close to my goal now.

I ask for your help and thank you very much in advance!


EDIT

Alright! Many thanks, everybody. With the 3 suggested modifications I ended up with:

// Lists all files in folder
File folder = new File(dir);
File fList[] = folder.listFiles();
// Searchs .lck
for (int i = 0; i < fList.length; i++) {
    String pes = fList[i];
    if (pes.endsWith(".lck")) {
        // and deletes
        boolean success = (new File(fList[i]).delete());
    }
}

And now it works!

2022 version:

public static boolean deleteAllFilesWithSpecificExtension(String pathToDir, String extension) {
        boolean success = false;
        File folder = new File(pathToDir);
        File[] fList = folder.listFiles();
        for (File file : fList) {
            String pes = file.getName();
            if (pes.endsWith("." + extension)) {
                success = (new File(String.valueOf(file)).delete());
            }
        }
        return success;
    }

Upvotes: 11

Views: 24549

Answers (6)

joe100
joe100

Reputation: 1

Java 8 lambda

File folder = new File(yourDirString);
Arrays.stream(folder.listFiles())
            .filter(f -> f.getName().endsWith(".lck"))
            .forEach(File::delete);

Upvotes: 0

Yasmeen Azhar
Yasmeen Azhar

Reputation: 31

Final Code which works :)

File folder = new File(dir);
                File fList[] = folder.listFiles();

                for (File f : fList) {
                    if (f.getName().endsWith(".png")) {
                        f.delete(); 
                    }}

Upvotes: 3

Johnny Willer
Johnny Willer

Reputation: 3917

Java 8 approach

Arrays.stream(yourDir.listFiles((f, p) -> p.endsWith("YOUR_FILE_EXTENSION"))).forEach(File::delete);    

Upvotes: 5

tucuxi
tucuxi

Reputation: 17945

for (File f : folder.listFiles()) {
    if (f.getName().endsWith(".lck")) {
        f.delete(); // may fail mysteriously - returns boolean you may want to check
    }
}

Upvotes: 12

Yogendra Singh
Yogendra Singh

Reputation: 34367

You are using Collection method get on an Array. Use Array Index notation as below:

        File pes = fList[i];

Also better to use endsWith() String method over the file name as:

   if (pes.getName().endsWith(".lck")){
      ...
   }

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

fList.get(i) should be fList[i] as fList is an array, and it returns a File reference not a String.

Change: -

String pes = fList.get(i);

to: -

File pes = fList[i];

And then change if (pes.contains(".lck") == true) to
if (pes.getName().contains(".lck"))

In fact, since you are checking for the extension, you should use endsWith method rather than contains method. And yes, you don't need to compare your boolean value with ==. So just use this condition: -

if (pes.getName().endsWith(".lck")) {
    boolean success = (new File(fList.get(i)).delete());
}

Upvotes: 7

Related Questions