Comparing File Contents of Files on Google Drive using Google Apps Script

Gists

This is a sample script for comparing the file contents of files on Google Drive using Google Apps Script.

Sample script

Before you use this script, please enable Drive API at Advanced Google services. And also, please set the file IDs you want to check whether the file contents of the files are the same.

function checkFiles_(f, checks = ["md5Checksum", "sha1Checksum", "sha256Checksum"]) {
  files = f.map(id => DriveApp.getFileById(id));
  const fields = [...checks, "id"].join(",");
  const o = files.reduce((o, f) => {
    const mimeType = f.getMimeType();
    if (["FOLDR", "SHORTCUT"].some(m => mimeType == MimeType[m])) {
      throw new Error("Folders cannot be checked.");
    }
    let obj;
    if (mimeType.includes("application/vnd.google-apps")) {
      const name = f.getName();
      f.setName("temp");
      Utilities.sleep(2000);
      obj = Drive.Files.insert({ title: "temp", mimeType: MimeType.PDF }, f.getBlob(), { supportsAllDrives: true, fields });
      f.setName(name);
      Drive.Files.remove(obj.id); // If an error occurs, please use DriveApp.getFileById(obj.id).setTrashed(true);
    } else {
      obj = Drive.Files.get(f.getId(), { supportsAllDrives: true, fields });
    }
    checks.forEach(e => o[e] = o[e] ? [...o[e], obj[e]] : [obj[e]]);
    return o;
  }, {});
  return Object.values(o).every(e => [...new Set(e)].length == 1);
}

// Please run this function.
function main() {
  const file1 = "###fileId1###"; // Please set your file ID of file 1.
  const file2 = "###fileId2###"; // Please set your file ID of file 2.

  const check = checkFiles_([file1, file2]);
  const res = `"${file1}" and "${file2}" are${
    check ? " " : " not "
  }the same data.`;
  console.log(res);
}
  • When this script is run, when the file contents of your inputted files are the same, true is returned.

  • This sample script uses the values of “md5Checksum”, “sha1Checksum”, “sha256Checksum” for comparing the files. If you want to use only “md5Checksum”, please modify const check = checkFiles_([file1, file2]); to const check = checkFiles_([file1, file2], ["md5Checksum"]);.

Note

  • This sample script can check only the file content. This script doesn’t check the file metadata. Please be careful about this.

  • This sample script cannot use the folder, the shortcut and so on. If you noticed other mimeTypes this script cannot use, please modify if (["FOLDR", "SHORTCUT"].some((m) => mimeType == MimeType[m])) {.

  • Recently, sha1Checksum and sha256Checksum have been added. By this, these values are not returned as the default returned values. Please be careful about this.

  • Of course, the values of sha1Checksum and sha256Checksum can be calculated using Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, bytes) and Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, bytes). But, in this case, it depends on the file size. For example, when the file size is more than 50 MB, an error like exceeds the maximum file size. occurs. Please be careful about this. And also, the process cost becomes high. For example, when I tested a file with 20 MB, the processing time for calculating sha256Checksum is about 15 seconds. On the other hand, when sha256Checksum is directly retrieved by Drive API, it is about 0.1 seconds.

Reference

 Share!