Class FileIterator

  • FileIterator allows scripts to iterate over potentially large collections of files.

  • File iterators can be accessed from DriveApp or a Folder.

  • ThehasNext() method determines if there are more files to iterate over.

  • Thenext() method retrieves the next file in the collection.

  • ThegetContinuationToken() method provides a token to resume iteration later.

FileIterator

An iterator that allows scripts to iterate over a potentially large collection of files. Fileiterators can be accessed fromDriveApp or aFolder.

// Log the name of every file in the user's Drive.constfiles=DriveApp.getFiles();while(files.hasNext()){constfile=files.next();Logger.log(file.getName());}

Methods

MethodReturn typeBrief description
getContinuationToken()StringGets a token that can be used to resume this iteration at a later time.
hasNext()BooleanDetermines whether callingnext() will return an item.
next()FileGets the next item in the collection of files or folders.

Detailed documentation

getContinuationToken()

Gets a token that can be used to resume this iteration at a later time. This method is usefulif processing an iterator in one execution would exceed the maximum execution time.Continuation tokens are generally valid for one week.

Return

String — a continuation token that can be used to resume this iteration with the items that remained in the iterator when the token was generated


hasNext()

Determines whether callingnext() will return an item.

Return

Booleantrue ifnext() will return an item;false if not


next()

Gets the next item in the collection of files or folders. Throws an exception if no itemsremain.

Return

File — the next item in the collection

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.