I’m trying to pull file information from a Google Drive folder and put it into a Google Sheets document. I found some code online but when I run it, I get this error message:
Cannot find function next in object Files. (line 2, file “Code”)
You’re mixing up two different Google Apps Script patterns. getFoldersByName() returns a FolderIterator, so .next() works fine there. But getFiles() also returns a FileIterator - not an array - which is why your for loop breaks. I’ve hit this exact issue before when working with older GAS examples that used deprecated methods. With iterators, you need to check hasNext() and call next() repeatedly. Quick heads up: if your folder name isn’t unique, getFoldersByName() might grab the wrong one since it returns all folders with that name. Try getFolderById() instead if you have the specific folder ID - it’s way more reliable for production scripts.
Same bug hit me! fileList isn’t actually an array, so you can’t use fileList[j] or .length on it. Convert the iterator to an array first: var filesArray = []; while(fileList.hasNext()) { filesArray.push(fileList.next()); } Then your original loop works fine with filesArray.
You’re treating the FileIterator like a regular array, but it’s not. getFiles() returns a FileIterator object, so .length and bracket notation won’t work. Use the iterator methods instead: while (fileList.hasNext()) { currentFile = fileList.next(); }. It’s a common mistake with Google Apps Script - their iterators work differently than normal JavaScript arrays. Plus, iterators are more memory efficient for big folders since they process files one at a time instead of loading everything at once.