List all files id inside a folder (no subfolders)
25,495
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);
}
Author by
miodf
Updated on December 02, 2020Comments
-
miodf over 3 years
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
-
miodf over 9 yearsMany 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. ;)
-
New_2_Code over 5 yearsThanks Serge, your script dropped the execution time from 30 min (maxed out) to 120 seconds. ;D
-
Serge insas over 5 yearsYou just need to use another while loop on top of the file's one using DriveApp.getFolders()...
-
user2240778 over 5 yearsI 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.
-
Serge insas over 5 yearsPut it the the other way, loop through the folders and inside that loop look for the files and get their names and sizes.
-
user2240778 over 5 yearsI'm sorry, I'm not sure of this, this is what I have and its only giving me all the folder ID's but not file IDs within the folders. - while (subfolders.hasNext()){ file = subfolders.next(); var row = [] row.push(file.getName(),file.getId(),file.getSize()) list.push(row); while (files.hasNext()){ file = files.next(); var row = [] row.push(file.getName(),file.getId(),file.getSize()) list.push(row); } }
-
Serge insas over 5 yearsI'll write an update in another answer, it will be easier to read
-
user2240778 over 5 yearsLet us continue this discussion in chat.
-
DanCue about 5 years@Sergeinsas Did you write up the solution with folders on another answer? Can you provide that link?
-
Serge insas about 5 yearssorry for the late answer... stackoverflow.com/questions/15813879/…
-
DanCue about 5 years@Sergeinsas Thank you. The linked solution lists only folders. I need a solution to do both files and folders. I think I will be posting a question for it.