I'm trying to figure out a way to delete files that windows duplicates when making multiple copies. I was able to do something by creating the code below:
import java.util.*;
import java.io.*;
public class FileCreator{
public static void main(String[] args) throws Exception{
File f = f = new File(".");
File[] files = f.listFiles();
for(File fl : files){
String fileName = fl.getName();
if(fileName.contains("Copia - Copia")){
System.out.println(fileName);
}
}
}
}
I have created some files, as follows the print below:
Andtheresultwas:
C:\Users\diego\Desktop\Nova pasta>java FileCreator
File 0 - Copia - Copia.txt
File 10 - Copia - Copia.txt
File 12 - Copia - Copia.txt
File 14 - Copia - Copia.txt
File 16 - Copia - Copia.txt
File 18 - Copia - Copia.txt
File 2 - Copia - Copia.txt
File 4 - Copia - Copia.txt
File 6 - Copia - Copia.txt
File 8 - Copia - Copia.txt
This form even caters to me, since I just replace the text output of the condition within the loop with a simple fl.delete();
but I would like to have more control over what is deleted by using a regex.
I started to do something as below, but I could not create a regex that could detect " Copia - Copia
" exactly at the end of the file name, and then delete it.
Pattern p = Pattern.compile("");
Matcher m;
f = new File(".");
File[] files = f.listFiles();
for(File fl : files){
String fileName = fl.getName();
m = p.matcher(fileName);
if(m.find()){
//fl.delete();
System.out.println(fileName + " deletado");
}
}
How do I make a regex that filters these files?
Note: detecting the extension is irrelevant, I only need to detect the Copia - Copia
which is how windows renames duplicates of duplicates, adding at the end of the file name.