3
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 created some files, as follows the print below:
And the mourning was:
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 suits me, because I just replace the text output of the condition within the loop by a simple fl.delete();
but would like to have more control over what is excluded, using a regex.
I started to do something according to below, but I could not create a regex that can detect the "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 fills these aquivos?
Note: detecting the extension is irrelevant, I just need to detect the Copia - Copia
which is like windows renames duplicates of duplicates, adding at the end of the file name.
If possible, I would like to understand the functioning of the expression as well
Because you don’t: filename.split(".")[0]. substring(filename.length-13, filename.length) == "Copy - Copy"
– Douglas
I found the expression here: http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word
– Douglas
@Douglas intended to learn using regex because I also have an automated program here, where I just add a regex and set up some rules in the window and he does the rest. In string I know how to do but I think the folder where I will run maybe I don’t have permission to run(work network folder :/)
– user28595
@diegofm see if the explanation is clear in http://answall.com/a/167965/3635
– Guilherme Nascimento