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";
}