Last active
May 9, 2016 12:59
-
-
Save igormukhin/71d780c4274336eeb297 to your computer and use it in GitHub Desktop.
Groovy script to compare two directory trees and output the differences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Field | |
@Field int processed = 0; | |
def dir1 = new File(args[0]) | |
def dir2 = new File(args[1]) | |
def diff = compareDirs(dir1, dir2) | |
diff.each { | |
printFileInfo(it.left, dir1, "Left: ") | |
printFileInfo(it.right, dir2, "Right: ") | |
println it.reason | |
println "" | |
} | |
def printFileInfo(file, base, title) { | |
if (file != null) { | |
println title + (file.isDirectory() ? "D ": "F ") + base.toPath().relativize(file.toPath()) | |
} else { | |
println title + "-" | |
} | |
} | |
def compareDirs(dir1, dir2) { | |
def diff = [] | |
dir1.listFiles().each { | |
incementProgress(it, dir1) | |
def sameOnRight = new File(dir2, it.name) | |
if (!sameOnRight.exists()) { | |
diff << [left: it, right: null, reason: "Not found on the right"]; | |
} else if (it.isDirectory() && !sameOnRight.isDirectory()) { | |
diff << [left: it, right: sameOnRight, reason: "Left is a dir, right is a file"]; | |
} else if (!it.isDirectory() && sameOnRight.isDirectory()) { | |
diff << [left: it, right: sameOnRight, reason: "Left is a file, right is a dir"]; | |
} else if (it.isDirectory()) { | |
diff += compareDirs(it, sameOnRight) | |
} else { | |
if (it.length() != sameOnRight.length()) { | |
diff << [left: it, right: sameOnRight, reason: "Files have different length"]; | |
} else if (it.lastModified() != sameOnRight.lastModified()) { | |
diff << [left: it, right: sameOnRight, reason: "Files have different timestamps"]; | |
} | |
} | |
} | |
dir2.listFiles().each { | |
incementProgress(it, dir2) | |
def sameOnLeft = new File(dir1, it.name) | |
if (!sameOnLeft.exists()) { | |
diff << [left: null, right: it, reason: "Not found on the left"]; | |
} | |
} | |
return diff | |
} | |
def incementProgress(file, base) { | |
processed++ | |
System.err.println processed + ": " + base.toPath().relativize(file.toPath()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment