How to log all files inside a folder

152 views Asked by At

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());
    }
}
2

There are 2 answers

0
Nabnub On BEST ANSWER

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());     
  }  
}
1
Bart Crooijmans On

var files = DriveApp.getFolderById("specific folder id placed here"); this does not return files, it will return a folder

instead try something like:

var folder = DriveApp.getFolderById("specific folder id placed here");

var files = folder.getFiles()