Duplicate file finder

R

Richard Sandoz

rem c:\pix contains some weird hiearchy of files and folders you only
know
rem it will recursivly walk the directory structure, do a crc32
calculation
rem and compare the crc32 and make a folder called *.dup and place
said
rem duplicate in that folder
rem
rem all you need to do after running the following is do a search on
c:\pix
rem for all folders named *.dup and delete them (hence duplicate files
are gone)
rem
java -cp . CRC C:\pix

<<CRC.java>>
import java.io.*;
import java.util.zip.*;
import java.util.*;
import java.io.*;

class CRC {
static CRC32 crc = new CRC32();
static String source;
static Map map = new HashMap();

static private void processFile(File file) {
byte[] buf = new byte[32768];
int len;
try {
crc.reset();
FileInputStream fi = new FileInputStream(file);
while ((len = fi.read(buf)) > 0)
crc.update(buf);
fi.close();
File dup = (File)map.put(crc.getValue() + " " +
file.length(),file);
if (dup != null) {
String key = crc.getValue() + " " + file.length();
map.put(key,dup);
File dest = new File(dup.getParent() + File.separator +
dup.getName() + ".dup");
dest.mkdirs();
System.out.println(file + "--" + dest + File.separator +
file.getName());
file.renameTo(new File(dest + File.separator +
file.getName()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

static private void buildFileList(File path) {
File[] files = path.listFiles();

for (int i = 0; i < (files == null ? 0 : files.length); i++)
if (files.isDirectory())
buildFileList(files);
else
processFile(files);
}

static public void main(String[] args) throws IOException {
source = args[0];
buildFileList(new File(args[0]));
//ObjectOutputStream os = new ObjectOutputStream(new
FileOutputStream("map.txt"));
//os.writeObject(map);
//os.close();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top