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:
Can anyone help me out?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With