package com.atolsystems.atolutilities.tools;
import com.atolsystems.atolutilities.AFileChooser;
import com.atolsystems.atolutilities.AFileUtilities;
import com.atolsystems.atolutilities.ATextScreenOutput;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author seb
*/
public class FolderDiff {
static void showHelpAndExit(int exitStatus){
println("FolderDiff utility\n"+
"Compare a folder tree against a reference folder tree\n"+
"Create a copy of all files which are absent or different in the reference tree\n"+
"Usages: - Console mode: FolderDiff <referenceFolderName> <topFolderName>\n"+
" - GUI mode: FolderDiff"
);
if(guiMode){
while(true){
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException ex) {
Logger.getLogger(FolderDiff.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.exit(exitStatus);
}
static boolean guiMode=false;
static ATextScreenOutput logWin = null;
public static void main(String[] args) throws IOException {
File refFolder;
File topFolder;
File outFolder;
if(0==args.length){
logWin= new ATextScreenOutput(null, false, true);
logWin.setVisible(true);
guiMode=true;
refFolder=AFileChooser.askForDirectory("Choose reference folder", false);
if(null==refFolder)
showHelpAndExit(-1);
refFolder=refFolder.getCanonicalFile();
topFolder=AFileChooser.askForDirectory("Choose top folder", false);
if(null==topFolder)
showHelpAndExit(-1);
topFolder=topFolder.getCanonicalFile();
}else if(2==args.length){
refFolder = (new File(args[0])).getCanonicalFile();
topFolder = (new File(args[1])).getCanonicalFile();
}else{
showHelpAndExit(1);
return;
}
outFolder = (new File(topFolder.getParentFile(),refFolder.getName()+"_To_"+topFolder.getName())).getCanonicalFile();
println("Comparing "+refFolder+" (reference) to "+topFolder);
println("Output folder: "+outFolder);
folderDiff(refFolder,topFolder,outFolder);
println("done");
}
private static void print(String msg){
if(guiMode){
logWin.appendAndScroll(msg);
}else{
System.out.print(msg);
}
}
private static void println(String msg){
if(guiMode){
logWin.appendAndScroll(msg+"\n");
}else{
System.out.println(msg);
}
}
static int level=0;
private static void folderDiff(File refFolder, File topFolder, File outFolder) throws IOException {
for(int i=0;i<level;i++)
print(" ");
println("open "+topFolder.getCanonicalPath());
level++;
File[] list=topFolder.listFiles();
for(File inTop:list){
File inRef=new File(refFolder,inTop.getName());
File inOut=new File(outFolder,inTop.getName());
if(inTop.isDirectory()){
if(inRef.exists()){
if(inRef.isDirectory()) folderDiff(inRef,inTop,inOut);
else{
println("WARNING: a file in reference folder is replaced by a folder: "+inRef);
AFileUtilities.copyFolder(inOut,inTop);
}
}else{
println("copy folder "+inTop);
AFileUtilities.copyFolder(inOut,inTop);
}
}else{
if(inRef.exists()){
if(inRef.isFile()){
if(!AFileUtilities.compare(inRef, inTop)){
inOut.getParentFile().mkdirs();
println("copy "+inTop);
AFileUtilities.copyFile(inOut,inTop);
}
}else{
println("WARNING: a folder in reference folder is replaced by a file: "+inRef);
inOut.getParentFile().mkdirs();
println("copy "+inTop);
AFileUtilities.copyFile(inOut,inTop);
}
}else{
inOut.getParentFile().mkdirs();
println("copy "+inTop);
AFileUtilities.copyFile(inOut,inTop);
}
}
}
level--;
for(int i=0;i<level;i++)
print(" ");
println("close "+topFolder.getCanonicalPath());
}
}