0

I want to search my Google Drive for any files that are 217 bytes. I used this post to create a recursive folder iterator using a ContinuationToken. However, this method requires me to keep each execution of the script to less than 6 minutes to avoid Google Scripts maximum execution time. So, I have to manually restart the script multiple times, or I have to create a trigger to restart the script multiple times. I would prefer to avoid this route.

If I was searching for files by creation date or contents in the title, I could use DriveApp.searchFiles which accepts such criteria. However, the Google documentation doesn't indicate DriveApp.searchFiles accepts criteria for size.

Before I assume my only option is the 'recursive folder iterator using a ContinuationToken' route, I wanted to see if I was missing something which would allow me to use a more simple approach.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Although I'm not sure whether this is the direct solution, I proposed a sample script as an answer. Could you please confirm it? If that was not the direction you expect, I apologize. – Tanaike Oct 14 '20 at 05:06
  • Did my answer show you the result what you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. If you have issues for my answer yet, I apologize. At that time, can I ask you about your current situation? I would like to study to solve your issues. – Tanaike Oct 22 '20 at 00:13

1 Answers1

1

I believe your goal as follows.

  • You want to retrieve the file list of the files with the size of 217 bytes using Google Apps Script.
  • You want to reduce the process cost of the script.

Issue and workaround:

Unfortunately, the search query of searchFiles method has no value of size. By this, your goal cannot be directly achieved. But I would like to propose the following workaround.

  • When the file metadata has the value of file size, it is found that the files are not Google Docs files (Document, Spreadsheet, Slides and so on). Using this, the process cost can be reduced a little. And if the mimeTypes of files you want to retrieve are known, the file list can be also retrieved by searching the mimeType.
  • When a lot of files are retrieved using Class DriveApp, the process cost of it is higher than that using Drive API.

So, I would like to propose to retrieve the file list using the search query of mimeType with the method of "Files: list" in Drive API.

Sample script:

function myFunction() {
  const size = 217;  // Please set the file size. In your question, it's 217 bytes.
  
  const excludeMimeTypes = [MimeType.FOLDER, MimeType.GOOGLE_DOCS, MimeType.GOOGLE_DRAWINGS, MimeType.GOOGLE_FORMS, MimeType.GOOGLE_SHEETS, MimeType.GOOGLE_SITES, MimeType.GOOGLE_SLIDES, MimeType.GOOGLE_APPS_SCRIPT];
  const q = excludeMimeTypes.map(e => `mimeType!='${e}'`).join(" AND ");
  let res = [];
  let pageToken = "";
  do {
    const r = Drive.Files.list({q: q, maxResults: 1000, pageToken: pageToken});
    res = res.concat(r.items.filter(e => e.hasOwnProperty("fileSize") && e.fileSize == size));
    pageToken = r.nextPageToken || "";
  } while (pageToken);
  console.log(res.length);  // Here, you can see the number of files in the list.activate()
  console.log(JSON.stringify(res)); // Here, you can see the file list.
}

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165