Skip to content

Instantly share code, notes, and snippets.

@igormukhin
Last active May 9, 2016 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igormukhin/71d780c4274336eeb297 to your computer and use it in GitHub Desktop.
Save igormukhin/71d780c4274336eeb297 to your computer and use it in GitHub Desktop.
Groovy script to compare two directory trees and output the differences
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