tarleaa
15 years agoContributor
Compare two folders
Hello, is there a way in TC to compare 2 folders? I know that 2 files can be compared, but how about folders? Thanks, Andrei
function CompareTwoFolders(fldr1, fldr2)
{
var fso, f1, f2, fc1, fc2;
fso = Sys.OleObject("Scripting.FileSystemObject");
f1 = fso.GetFolder(fldr1);
fc1 = new Enumerator(f1.files);
f2 = fso.GetFolder(fldr2);
fc2 = new Enumerator(f2.files);
if (fc1.atEnd()) {
Log.Error("The " + fldr1 + " folder is empty!");
return false;
}
if (fc2.atEnd()) {
Log.Error("The " + fldr2 + " folder is empty!");
return false;
}
for (; !fc1.atEnd(); fc1.moveNext())
{
if (fc2.atEnd()) {
Log.Error("The number of files is different!");
return false;
}
filename1 = new String(fc1.item());
filename2 = new String(fc2.item());
filename1 = filename1.substr(filename1.lastIndexOf("\\") + 1);
filename2 = filename2.substr(filename2.lastIndexOf("\\") + 1);
if (filename1 != filename2)
return false;
if (!Files.Compare(fc1.item(), fc2.item()))
return false;
fc2.moveNext();
}
if (!fc2.atEnd()) {
Log.Error("The number of files is different!");
return false;
}
return true;
}
function Main()
{
if (CompareTwoFolders("C:\\temp1", "C:\\temp2")) {
Log.Message("The contents of the folders are the same!");
}
else {
Log.Error("The contents of the folders are not the same!");
}
}