Search code examples
google-apps-scriptgoogle-apps-script-editor

How to log all files inside a folder


I want to navigate into a specific folder and then print all the files inside that specified folder using Google Apps Script:

function listFilesinSpecFolder(){
    var files = DriveApp.getFolderById("specific folder id placed here");
    while (files.hasNext()) {
      var file = files.next();
      Logger.log(file.getName());
    }
}

When I tried to log this it gives me an error saying that hasNext() function is not a function. But when I run the below code it gives me no errors...

function folder(){
    var folders = DriveApp.getFolders();
    while (folders.hasNext()) {
      var folder = folders.next();
      Logger.log(folder.getName());
    }
}

Solution

  • The following script will help you 'to navigate into a specific folder and then print all the files inside that specified folder'

    CODE:

    function listFilesinSpecFolder() {
    
      var folder = DriveApp.getFolderById('FOLDER ID GOES HERE');
      var file;
      var contents = folder.getFiles();
      while(contents.hasNext()) {
        file = contents.next();    
        Logger.log(file.getName());     
      }  
    }