5

I've had a search but cannot seem to find what I am looking for.

I am seeking the code to enable me to do this pseudocode:

  1. Count number of files in specific Google Drive folder (by folder Id)
  2. If file count is above 'x' do this {other piece of code}
  3. else, exit

Can anyone help me out?

witham
  • 139
  • 1
  • 2
  • 13

1 Answers1

11

I can't find any method to give the count for the number of files, so I guess you'll need to loop through every file, and have a counter:

function countFilesInFolder() {
  var count,file,files,theFolder;//Define variables without assigning a value

  theFolder = DriveApp.getFolderById('your file ID');
  files = theFolder.getFiles();

  count = 0;

  while (files.hasNext()) {
   count++;
   file = files.next();
   //Logger.log(file.getName());
   if (count > 5) {
     fncNextFunction();
     break;
   };
 };

  Logger.log(count);
};

function fncNextFunction() {
  Logger.log('fncNextFunction');

};

Note: If I just have the counter increment without getting the next file, the script editor freezes.

Here is an alternative method using the advanced Drive Service:

//This requires the Drive API To be turned on in the Advanced Google Services
function countFilesInFolder() {

 var query = 'trashed = false and ' +
        "'Your Folder ID here' in parents";

  var filesInFolder = Drive.Files.list({q: query});

  Logger.log('filesInFolder: ' + filesInFolder.items.length);

  for (var i=0;i<filesInFolder.items.length;i++) {
    //Logger.log('key: ' + key);
    var thisItem = filesInFolder.items[i];
    Logger.log('value: ' + thisItem.title);
  };
};

For an example of using page tokens to list more than 100 results, see The Documentation

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • I'm not familiar with apps script so this might be off base, but I would probably recommend looking at the Advanced Drive Service. That allows a couple of efficiencies. Firstly, you can specify which file properties you want to have returned, which will minimise how much data needs to be shipped which should make things faster. In your case you don't actually need any of the properties, so (I'm guessing) you'll get an array of empty objects. Secondly, you can simply add the array count to an accumulator, then repeat for each pagetoken, so there won't be any need to iterate the files objects. – pinoyyid May 19 '15 at 18:55
  • I added an alternative method to the answer using the advanced Drive Service. – Alan Wells May 19 '15 at 20:29
  • you aren't looping through nextpagetoken so will only see a max of 100 files – pinoyyid May 19 '15 at 21:38
  • I'll mention that in the answer. There is no `maxResults` parameter specified in the search, so will it still automatically only get 100 files? [files.list](https://developers.google.com/drive/v2/reference/files/list) – Alan Wells May 19 '15 at 21:57
  • That's right, but being precise, it's up to 100 because the api spec doesn't guarantee that each page will be full. – pinoyyid May 19 '15 at 22:00
  • Thanks for your help & comments Sandy & pinoyyid. The bottom example with Drive API worked like a charm. I only need my code to fire if the folder is empty so the 100 file limit is not an issue in this instance - thanks both once again. – witham May 20 '15 at 13:32
  • @AlanWells, if I have several folderIDs from a google sheet and would like to loop it through and only count the total files, how do I change the query? I tried this [var query = 'trashed = false and ' + thisFolderID + " in parents"] --- but it won't work. – user7935276 Jul 11 '21 at 01:44
  • Without seeing your entire code, I can't know what the problem is. And I don't want to have an extended discussion in the comments section. You could ask a question in the [Apps Script Community](https://groups.google.com/g/google-apps-script-community) and provide your code, and a more detailed explanation of what is not working. – Alan Wells Jul 11 '21 at 03:26
  • Thank you. I will post my question in the apps scripts community. – user7935276 Jul 12 '21 at 16:01
  • Thank you. I already figured out the query. – user7935276 Jul 12 '21 at 18:31
  • If you comment back showing your solution maybe I could add it as extra information to the answer. – Alan Wells Jul 12 '21 at 19:48