Whatever message this page gives is out now! Go check it out!

FileMismatch

Last update:
May 18, 2026
Compares two files byte by byte and returns -1 if they are identical, or the 0-based byte position of the first mismatch.

Description

The function performs a byte-by-byte comparison of two files and returns a numeric result that identifies whether the files are identical or where they first differ.
This function provides a simple way to compare file contents without manually reading and comparing bytes.

Syntax

FileMismatch(String filePath1, String filePath2)
ParameterTypeDescription
filePath1String (required)The absolute or relative path to the first file.
filePath2String (required)The absolute or relative path to the second file.

Returns

Long: Returns -1 if the files are identical, or the 0-based byte position of the first difference.

Usage Notes

  • If files differ in size, the mismatch is reported at the point where one file ends.
  • The comparison is byte-based, so it works with text and binary files.
  • A return value of -1 means the two files match exactly.

Comparing Identical Files


<cfscript>
file1 = expandPath("./test1.txt");
file2 = expandPath("./test2.txt");

FileWrite(file1, "Hello World");
FileWrite(file2, "Hello World");

result = FileMismatch(file1, file2);
writeOutput("Result: " & result);
writeOutput("Files identical: " & (result == -1 ? "YES" : "NO"));
</cfscript>
Expected Output:
Result: -1
Files identical: YES

Detecting First Mismatch Position


<cfscript>
file1 = expandPath("./test1.txt");
file2 = expandPath("./test2.txt");

FileWrite(file1, "Hello World");
FileWrite(file2, "Hello Xorld");

result = FileMismatch(file1, file2);
writeOutput("Mismatch at byte position: " & result);
</cfscript>
Expected Output:
Mismatch at byte position: 6

Comparing Files with Completely Different Content


<cfscript>
file1 = expandPath("./same.txt");
file2 = expandPath("./different.txt");

FileWrite(file1, "Same content");
FileWrite(file2, "Different content");

result = FileMismatch(file1, file2);
writeOutput("Mismatch at byte position: " & result);
</cfscript>
Expected Output:
Mismatch at byte position: 0

Multi-Line File Comparison


<cfscript>
file1 = expandPath("./multi1.txt");
file2 = expandPath("./multi2.txt");

FileWrite(file1, "Hello World" & Chr(10) & "Line 2" & Chr(10) & "Line 3");
FileWrite(file2, "Hello World" & Chr(10) & "Line 2" & Chr(10) & "Line 4");

result = FileMismatch(file1, file2);
writeOutput("Mismatch at byte position: " & result);
</cfscript>
Expected Output:
Mismatch at byte position: 25

Loop-Created Identical Files


<cfscript>
file1 = expandPath("./loop1.txt");
file2 = expandPath("./loop2.txt");

content1 = "";
content2 = "";
for (i = 1; i <= 10; i++) {
    content1 &= "Line " & i & Chr(10);
    content2 &= "Line " & i & Chr(10);
}
FileWrite(file1, content1);
FileWrite(file2, content2);

result = FileMismatch(file1, file2);
writeOutput("Result: " & result);
writeOutput("Loop-created files identical: " & (result == -1 ? "YES" : "NO"));
</cfscript>
Expected Output:
Result: -1
Loop-created files identical: YES

Multiple File Comparisons


<cfscript>
testDir = expandPath("./testfiles");

files = [
    {name: "multi1.txt", content: "Same content"},
    {name: "multi2.txt", content: "Same content"},
    {name: "multi3.txt", content: "Different content"}
];

for (f in files) {
    FileWrite(testDir & "/" & f.name, f.content);
}

result12 = FileMismatch(testDir & "/multi1.txt", testDir & "/multi2.txt");
result13 = FileMismatch(testDir & "/multi1.txt", testDir & "/multi3.txt");

writeOutput("File1 vs File2: " & (result12 == -1 ? "IDENTICAL" : "DIFFERENT at " & result12));
writeOutput("File1 vs File3: " & (result13 == -1 ? "IDENTICAL" : "DIFFERENT at " & result13));
</cfscript>
Expected Output:
File1 vs File2: IDENTICAL
File1 vs File3: DIFFERENT at 0

Using FileMismatch Inside a Closure


<cfscript>
testDir = expandPath("./testfiles");

compareFiles = function(file1, file2) {
    return FileMismatch(file1, file2);
};

f1 = testDir & "/closure1.txt";
f2 = testDir & "/closure2.txt";

FileWrite(f1, "Closure test content");
FileWrite(f2, "Closure test content");

result = compareFiles(f1, f2);
writeOutput("FileMismatch via closure: " & result);
writeOutput("Closure comparison works: " & (result == -1 ? "YES" : "NO"));
</cfscript>
Expected Output:
FileMismatch via closure: -1
Closure comparison works: YES

UDF Wrapper — Boolean Check


<cfscript>
function areFilesIdentical(required string path1, required string path2) {
    var result = FileMismatch(path1, path2);
    return result == -1;
}

f1 = expandPath("./udf1.txt");
f2 = expandPath("./udf2.txt");

FileWrite(f1, "UDF test content");
FileWrite(f2, "UDF test content");

isIdentical = areFilesIdentical(f1, f2);
writeOutput("Files identical (via UDF): " & (isIdentical ? "YES" : "NO"));
</cfscript>
Expected Output:
Files identical (via UDF): YES

UDF Wrapper — Descriptive Mismatch


<cfscript>
function describeMismatch(required string path1, required string path2) {
    var result = FileMismatch(path1, path2);
    if (result == -1) {
        return "Files are identical";
    } else {
        return "First difference at byte " & result;
    }
}

f1 = expandPath("./describe1.txt");
f2 = expandPath("./describe2.txt");

FileWrite(f1, "Hello World");
FileWrite(f2, "Hello Xorld");

writeOutput("Comparison result: " & describeMismatch(f1, f2));
</cfscript>
Expected Output:
Comparison result: First difference at byte 6

Array Filter — Find All Files Matching a Baseline


<cfscript>
testDir = expandPath("./testfiles");

files = [];
for (i = 1; i <= 4; i++) {
    path = testDir & "/array_test" & i & ".txt";
    FileWrite(path, "Common content");
    ArrayAppend(files, path);
}

baseFile = files[1];
matchingFiles = files.filter(function(f) {
    return FileMismatch(baseFile, f) == -1;
});

writeOutput("Total files: " & ArrayLen(files));
writeOutput("Matching files: " & ArrayLen(matchingFiles));
</cfscript>
Expected Output:
Total files: 4
Matching files: 4

Array Map — Comparison Results for Multiple Files


<cfscript>
testDir = expandPath("./testfiles");

FileWrite(testDir & "/var1.txt", "Content A");
FileWrite(testDir & "/var2.txt", "Content A");
FileWrite(testDir & "/var3.txt", "Content B");
FileWrite(testDir & "/var4.txt", "Content A");

files = [
    testDir & "/var1.txt",
    testDir & "/var2.txt",
    testDir & "/var3.txt",
    testDir & "/var4.txt"
];

baseFile = files[1];
results = files.map(function(f) {
    return {file: ListLast(f, "/"), result: FileMismatch(baseFile, f)};
});

identicalCount = 0;
for (r in results) {
    if (r.result == -1) identicalCount++;
}

writeOutput("Files identical to base: " & identicalCount);
</cfscript>
Expected Output:
Files identical to base: 3

Counting Identical Pairs Across Multiple Files


<cfscript>
testDir = expandPath("./testfiles");

files = [];
for (i = 1; i <= 3; i++) {
    path = testDir & "/reduce" & i & ".txt";
    FileWrite(path, "Same content for reduce test");
    ArrayAppend(files, path);
}

pairCount = 0;
identicalPairs = 0;
for (i = 1; i <= ArrayLen(files); i++) {
    for (j = i + 1; j <= ArrayLen(files); j++) {
        pairCount++;
        if (FileMismatch(files[i], files[j]) == -1) {
            identicalPairs++;
        }
    }
}

writeOutput("Total pairs: " & pairCount);
writeOutput("Identical pairs: " & identicalPairs);
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page