Fastest way to count all files in a folder (including files in subfolders)

1,191 views
Skip to first unread message

jmm0979

unread,
Jul 13, 2021, 12:12:04 PM7/13/21
to google-apps-sc...@googlegroups.com
Hi,

Is there a way to count all files in a folder (including files in subfolders)? I only need a total count without listing the file information. I am using the code below but noticed that it does not provide the entire file count.
I hope someone here can help here. TIA!

function countFilesInFolder() {

var ss = SpreadsheetApp.getActive().getSheetByName('SHEET NAME HERE);
var values = ss.getRange(2, 1, ss.getLastRow(), ss.getLastColumn()).getValues();

for (var i = 0; i < values.length - 1; i++) {
var thisFolderID = values[i][0];
var filecount = values[i][1];

if (filecount != "") { continue; }
if (filecount == "") {

var query = 'trashed = false';
// var query = ' "' + thisFolderID + '" in parents and trashed = false';

var filesInFolder = Drive.Files.list({
q: query,
'folderId': thisFolderID,
});
var totalFiles = filesInFolder.items.length;
ss.getRange(i + 2, 2).setValue(totalFiles);
}
}
}



Andrew Roberts

unread,
Jul 13, 2021, 1:35:34 PM7/13/21
to google-apps-sc...@googlegroups.com
Take a look at the ListFiles_ object in this "delete files" script.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAHL69DVQgw1y7QqKthwhEtL502_1Oo5avm2QvZzSfJKHkup-8w%40mail.gmail.com.

jmm0979

unread,
Jul 13, 2021, 8:58:21 PM7/13/21
to google-apps-sc...@googlegroups.com
Hi Andrew,

I checked the script but it looks like it is listing all files but I am only looking for the total count of all the files in a folder (including files within the subfolders).

dimud...@gmail.com

unread,
Jul 14, 2021, 7:42:33 AM7/14/21
to Google Apps Script Community
@JMR Unfortunately, the Drive APIs do not seem to provide a native data point that stores the file count. I suspect Andrew was suggesting that you look at the ListFiles implementation and modify it to meet your needs since you'll need to list your drive files to calculate the count.  

Clark Lind

unread,
Jul 14, 2021, 7:50:07 AM7/14/21
to Google Apps Script Community
Tanaike also has a library that lists files and folders under a specific folder (here). I think maybe the simplest thing to do is add a small modification to what is returned from these functions. In Andrew's where it says:

return ns  , try simply adding  return ns.length  or console.log(ns.length) etc.  I'm guessing these functions are building an array of all files and folder names, so all you need to do is get the length of that array.

Hope that helps a little.

johan

unread,
Jul 14, 2021, 9:30:19 AM7/14/21
to google-apps-sc...@googlegroups.com
buenas como estan, soy un poco nuevo ya reconocer algunos tipos de coneccion a google sheet, pero queria saber si me pueden orientar a hacer lo siguiente 
1) hacer una aplicación web que pueda incrustar en google site con las siguientes características 
a) Conectarse a google sheet como base de datos 
b) realizar un index donde se muestran varias card con una imagen título descripción y un botón de hipervínculo 
c)  los datos extraídos desde la hoja de google sheet, puedan ser editables  y mostrados en la página (index)


Clark Lind

unread,
Jul 15, 2021, 11:29:52 AM7/15/21
to Google Apps Script Community
Publique esto como una publicación nueva. De esa manera, todos lo verán en lugar de estar dentro de otra publicación.

Bruce Mcpherson

unread,
Jul 17, 2021, 11:43:22 AM7/17/21
to Google Apps Script Community
There's a method to do this in one of my libraries - so it's kind of a one liner - for example
const d = () => { 
  // force a permission dialog with this comment 
 // DriveApp.getRootFolder() 
  const ff = bmFolderFun.paths(DriveApp) 
  console.log(ff.pileOfFiles({ includeSubfolders: true, start: 'Published Scripts', mimeTypes: MimeType.GOOGLE_APPS_SCRIPT }).length) }
}

Lilian Caruana

unread,
Nov 10, 2022, 8:04:29 AM11/10/22
to Google Apps Script Community
This looks really promising for my use case! I've been trying your solution out and I think I got it to work on My Drive, but it seems once moved to a shared NW drive, it no longer works. Is there a way to make it play nice in a shared drive too?

Thanks,
Lilian
Reply all
Reply to author
Forward
0 new messages