-3

I want to get file details from Google Drive, and store them in a Google Spreadsheet. I found this code but it gives an error:

Cannot find function next in object Files. (line 2, file "Code")

Code is from this blog, possibly evolved from this gist.

/**
 * List all files in Google Drive folder.
 *
 * http://ctrlq.org/code/19854-list-files-in-google-drive-folder
 * https://gist.github.com/hubgit/3755293
 */
function listFilesInFolder(folderName) {

  var folder = DriveApp.getFoldersByName(folderName).next();
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);

  for (var i = 0; i < contents.length; i++) {

    file = contents[i];

    if (file.getFileType() == "SPREADSHEET") {
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      file.getFileType().toString()
    ];

    sheet.appendRow(data);

  }

};
1
  • The code you linked will not run as is. It is a method that will be called from somewhere else in the code that passes the folder name to it. You can modify it to hardcode the folder name. Apr 30, 2015 at 12:14

2 Answers 2

2

As Sandy's answer points out, the error message you've received is because no folder object is found by DriveApp.getFoldersByName(folderName).

However, you'll still have a problem, because the script you reference was only partially converted to use DriveApp when FilesList was deprecated.

  • It is treating the result of folder.getFiles() as an array, when it is actually an iterator.

  • DriveApp files don't have the getFileType() method, instead they support getMimeType(), and there is a MimeType enumerator to compare to.

  • The download URL in that code no longer works. You are able to get an export link from the Advanced Drive API - I've just commented out the offending code.

Code

A more complete version of this code is available in this gist.

/**
 * List all files in Google Drive folder.
 *
 * @param {string} folderName    (optional) Name of folder on Google Drive
 *
 * Adapted from:
 * http://ctrlq.org/code/19854-list-files-in-google-drive-folder
 * https://gist.github.com/hubgit/3755293
 */
function listFilesInFolder(folderName) {
  // If we have not been provided a folderName, assume we will interact with user.
  var interactive = (typeof folderName === 'undefined');

  // Get name of folder to list
  if (interactive) {
    folderName = Browser.inputBox("List files in folder", "Enter folder name", Browser.Buttons.OK_CANCEL);
  }

  if (folderName === '') return;  // No name provided, exit quietly

  var folders = DriveApp.getFoldersByName(folderName);
  if (!folders.hasNext()) {
    if (interactive) Browser.msgBox("Folder not found.");
    return;
  }
  var folder = folders.next();
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date", "Size", "URL", /*"Download",*/ "Description", "Type"]);

  // Loop over files in folder, using file iterator
  while (contents.hasNext()) {
    file = contents.next();

    if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
      // Skip displaying spreadsheets - I don't know why...
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      //"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      niceFileType( file.getMimeType() )
    ];

    sheet.appendRow(data);

  }
}

niceFileType Helper function

/**
 * Get nice, readable file type matching given MimeType. 
 * Refer to https://developers.google.com/apps-script/reference/base/mime-type
 *
 * @param {string} mimeType  File type to match
 *
 * @return {string}          "UNKNOWN" or matched file type.
 */
function niceFileType( mimeType ) {

  // Supported file types stored in global fileType object for fast lookups.
  if (typeof this.fileType === 'undefined') {
    // Initialize global fileType object first time only.
    this.fileType = {};
    // ...
    this.fileType[MimeType.FOLDER] = "Folder";
    // ...
    this.fileType[MimeType.GOOGLE_APPS_SCRIPT] = "Google Apps Script";
    this.fileType[MimeType.GOOGLE_DOCS] = "Google Doc";
    this.fileType[MimeType.GOOGLE_DRAWINGS] = "Google Drawing";
    this.fileType[MimeType.GOOGLE_FORMS] = "Google Form";
    this.fileType[MimeType.GOOGLE_SHEETS] = "Google Sheet";
    // ...
  }

  // If we know the given filetype, return its name.
  return (this.fileType.hasOwnProperty(mimeType)) ? this.fileType[mimeType] : "UNKNOWN";
}
2
  • the code posted really helped...But can you assist me with debugging a existing code which was used for old version of google drive...below is the link for the same.docs.google.com/a/searce.com/spreadsheets/d/…
    – Rudra
    May 4, 2015 at 5:10
  • 1
    No, sorry, that's out of scope.
    – Mogsdad
    May 4, 2015 at 12:10
0

You could change this:

function listFilesInFolder(folderName) {

  var folder = DriveApp.getFoldersByName(folderName).next();

To this:

function listFilesInFolder() {

  var folderName = "type your folder name here between the quotes";
  var folder = DriveApp.getFoldersByName(folderName).next();

Obviously, you are running the code, so "hard code" the folder name by typing your folder name into the statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.