Create a gist now

Instantly share code, notes, and snippets.

List all files in a folder (Google Apps Script)
function listFilesInFolder() {
var folder = DocsList.getFolder("Maudesley Debates");
var contents = folder.getFiles();
var file;
var data;
var 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(),
"audio/mp3"
];
sheet.appendRow(data);
}
};
@mike018
mike018 commented May 20, 2014

Hi Hubgit,

Is there a way also to get all files inside a sub folder? let's say in the above code, "Maudesley Debates" is the main folder then assuming that there is a sub folder named "Sub1" and "Sub2"
Sub1 and Sub2 has 3 files each inside.

so how to I append that in the spreadsheet as well..

further, is there a way that folder will be list down in a new column like

File Name | Folder Name
testfile I Maudesley Debates
testfile2 | Maudesley Debates,Sub1
testfile3 | Maudesley Debates,Sub1
testfile4 | Maudesley Debates,Sub2

so basically I want to list down all the folder the file is connected into and separate it with comma.

Hope to hear something from you the soonest as I'm really stuck with this challenge and i'm really hoping that you could help me out.

Please advise..
Thanks in advance!

@kishaningithub

@mike018 You found the answer to your question? Im also facing the same issue :-(

@tdries
tdries commented Feb 2, 2017

I found this awesome script which is working, BUT....

Has anyone any idea how to change it so it does not stop after 5 minutes when scanning large folders? (google script time-out).

Thank you very much!!!

==============================================

// TODO: Set folder ID

var folderId = 'My folder ID';

// Main function 1: List all folders, & write into the current sheet.

function listFolers(){

getFolderTree(folderId, false);

};

// Main function 2.

function listAll(){

getFolderTree(folderId, true);

};

// Get Folder Tree

function getFolderTree(folderId, listAll) {

try {

// Get folder by id

var parentFolder = DriveApp.getFolderById(folderId);

// Initialise the sheet

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

sheet.clear();

sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]);

// Get files and folders

getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);

} catch (e) {

Logger.log(e.toString());

}

};

// Get the list of files and folders and their metadata in recursive mode

function getChildFolders(parentName, parent, data, sheet, listAll) {

var childFolders = parent.getFolders();

// List folders inside the folder

while (childFolders.hasNext()) {

var childFolder = childFolders.next();

// Logger.log("Folder Name: " + childFolder.getName());

data = [

parentName + "/" + childFolder.getName(),

childFolder.getName(),

childFolder.getDateCreated(),

childFolder.getUrl(),

childFolder.getLastUpdated(),

childFolder.getDescription(),

childFolder.getSize()

];

// Write

sheet.appendRow(data);

// List files inside the folder

var files = childFolder.getFiles();

while (listAll & files.hasNext()) {

var childFile = files.next();

// Logger.log("File Name: " + childFile.getName());

data = [

parentName + "/" + childFolder.getName() + "/" + childFile.getName(),

childFile.getName(),

childFile.getDateCreated(),

childFile.getUrl(),

childFile.getLastUpdated(),

childFile.getDescription(),

childFile.getSize()

];

// Write

sheet.appendRow(data);

}

// Recursive call of the subfolder

getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);

}

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment