title: Searching for the newest file in a specific google drive folder
body: <p>I'm fairly new to coding in javascript and I'm trying to write a script which finds the latest csv file in a folder in google drive, and copies the contents to an existing sheet below the last row of data. </p>
  
  <p>I am getting an error which states the following:</p>
  
  <p>TypeError: Cannot find function hasNext in object Fri May 10 2019 16:42:55 GMT+1000 (AEST). (line 11, file "Code")</p>
  
  <p>Any help would be much appreciated!</p>
  
  <p>CODE:</p>
  
  <pre><code>   function getCSV() {
  var fSource = DriveApp.getFolderById('1mwVqOXikSBh4mcr4iowzJTa4ZvNnzO12'); //       reports_folder_id = id of folder where csv reports are saved
  var fi = fSource.getFilesByType('text/csv');// latest       report file
  
    let lastUpdatedFile = null;
  while(fi.hasNext()){
   if(!lastUpdatedFile){ //Checks if we didn't assign anything to lastUpdatedFile
     lastUpdatedFile = fi.next();
   }else{
    var currentFile = fi.next();
    //lastUpdatedFile is older than the current file
    //set lastUpdatedFile to the current file
    if(lastUpdatedFile.getLastUpdated() &lt; currentFile.getLastUpdated()){
      lastUpdatedFile = currentFile;
    }
   }
  }
  var ss    =SpreadsheetApp.openById('1W2-83TlLONomkcKtaH6f0Zcm4G9jJ1DdWn9BXBhKo2s'); //      data_sheet_id = id of spreadsheet that holds the data to be updated with new      report data
  var sd = SpreadsheetApp.getActiveSpreadsheet();
  var s = SpreadsheetApp.getActiveSheet();
  var target = new Array()
  var sSheet = SpreadsheetApp.getActiveSpreadsheet();
  sheet = SpreadsheetApp.setActiveSheet(sSheet.getSheets()[0]);
  
  if ( fi.hasNext()) {
  var file = fi.next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  
  var sheet = ss.getSheets()[0];
  var lastrow = s.getLastRow();
  s.getRange(lastrow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);
  s.getRange(+1, 1, csvData.length, csvData[5].length).setValues(csvData);
  }
  }
  </code></pre>