How can I list in a spreadsheet all files id that are located inside a folder ? I have the folder id and the folder contains less than 100 files in total.
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById('0sdfsfd......sfdsdfTg'); // I change the folder ID here
// Logger.log('Folder name: ' + folder.getFiles());
sh.getRange(1,1,1,1).setValues('fileID');
}
Thanks
This is quite simple if you read the documentation about how to write in a spreadsheet and how to get Drive's content.
Here is an example you can customize if you want :
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById('0B3qSFd3iikE3TERRSHExa29SU3M'); // I change the folder ID here
var list = [];
list.push(['Name','ID','Size']);
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
var row = []
row.push(file.getName(),file.getId(),file.getSize())
list.push(row);
}
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}
Many thanks. ;) I have spent 2 hours trying to find a way to do this. And finally I got lost ! Hence my simple question and your very helpful answer that will help other beginners in the future for sure. Thanks again. ;)
Thanks Serge, your script dropped the execution time from 30 min (maxed out) to 120 seconds. ;D
You just need to use another while loop on top of the file's one using DriveApp.getFolders()...
I added this above - while (files.hasNext()){.... var subfolder = DriveApp.getFolders() while (subfolder.hasNext()){ file = subfolder.next(); var row = [] row.push(file.getName(),file.getId(),file.getSize()) list.push(row); } It give me the sub folder ID's how do I get the file ID's within the sub folders.
Put it the the other way, loop through the folders and inside that loop look for the files and get their names and sizes.