-1

I want to add this code in my java utility programm, i wil call this later in another program.

This code will delete files older than 2 days. i have the following error at line 3 "Delete cannot be resolved as a type"

Any help, please, Thank you

public void delete(long days, String fileExtension) {
String dirPath = "c:\\Folder";
Delete deleteFiles = new Delete();
deleteFiles.delete(2, ".pdf");
File folder = new File(dirPath);

if (folder.exists()) {

File[] listFiles = folder.listFiles();

long eligibleForDeletion = System.currentTimeMillis()
                    - (days * 24 * 60 * 60 * 1000L);
System.out.println("Starting to clean ...");

for (File listFile : listFiles) {


if (listFile.getName().endsWith(fileExtension)
&& listFile.lastModified() < eligibleForDeletion) {

System.out.println("Deleted = " +listFile);

if (!listFile.delete()) {

System.out.println("Unable to Delete Files..");

    }
   }
  }
 }
}
user3610075
  • 73
  • 2
  • 4
  • 8

2 Answers2

0

You can use String.startsWith() inside the loop:

for (int index = 0; index < files.length; index++) {

    String s = files[index].toString();

    if (s.startsWith("report"))
        System.out.println(s);
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
0

You can use the String method:

startsWith(String prefix)

So in your code you can do this:

if(files[index].toString().startsWith("Report")) {
    //print
}
Arash Saidi
  • 2,228
  • 20
  • 36