title: cannot read property "0" of undefined error
body: <p>I have a code that pulls information from all excel type files in some folders on google drive. The problem is that there are over 100 files and the code only pulls data from around 30 files and shows the following error: &quot;TypeError: Cannot read property '0' of undefined&quot; The error is in line 12, &quot;console.log(lista_carpetas_ok2[i][0]) // Here, you can see the folder ID in the log.&quot;</p>
  <pre><code>function listfechas() {
    var ss2 = SpreadsheetApp.getActiveSpreadsheet();
    var carpetasSheet = ss2.getSheetByName(&quot;carpetas&quot;);
    var lista_carpetas = carpetasSheet.getRange(&quot;C2:C&quot; + carpetasSheet.getLastRow()).getValues();
    var lista_carpetas_ok2 = lista_carpetas.filter(([a]) =&gt; a);
    var sheet2 = ss2.getSheetByName(&quot;SS23&quot;);
    sheet2.clear();
    sheet2.appendRow([&quot;Folder&quot;, &quot;Name&quot;, &quot;SMS&quot;,&quot;rec SMS&quot;,  &quot;FIT&quot;,  &quot;rec FIT&quot;,  &quot;2FIT&quot;, &quot;rec 2FIT&quot;  ,&quot;3FIT&quot; ,&quot;rec 3FIT&quot; ,&quot;PP&quot;   ,&quot;rec PP&quot;   ,&quot;2PP&quot;  ,&quot;rec 2PP&quot;, &quot;3PP&quot;,  &quot;rec 3PP&quot;,&quot;SHIP&quot;,   &quot;rec SHIP&quot;, &quot;2SHIP&quot;,&quot;rec 2SHIP&quot;]);
    
  
   for (var i = 0; i &lt; 5; i++) {
      console.log(lista_carpetas_ok2[i][0]) // Here, you can see the folder ID in the log.
      var folderid = lista_carpetas_ok2[i][0];
    try {
      var parentFolder =DriveApp.getFolderById(folderid);
      listFiles(parentFolder,parentFolder.getName())
      listSubFolders(parentFolder,parentFolder.getName());
    } catch (e) {
      Logger.log(e.toString());
    }
  }
  
  function listSubFolders(parentFolder,parent) {
    var childFolders = parentFolder.getFolders();
    while (childFolders.hasNext()) {
      var childFolder = childFolders.next();
      Logger.log(&quot;Fold : &quot; + childFolder.getName());
      listFiles(childFolder,parent)
      listSubFolders(childFolder,parent + &quot;|&quot; + childFolder.getName());
    }
  }
  
  function listFiles(fold,parent){
    var data = [];
    var files = fold.getFilesByType(MimeType.GOOGLE_SHEETS);
     try {
    while (files.hasNext()) {
      var file = files.next();
        var sms = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;A2&quot;).getValue();
        var recsms = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;B2&quot;).getValue();
        var fit = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;C2&quot;).getValue();
        var recfit = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;D2&quot;).getValue();
        var dfit = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;E2&quot;).getValue();
        var recdfit = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;F2&quot;).getValue();
        var tfit = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;G2&quot;).getValue();
        var rectfit = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;H2&quot;).getValue();
        var pp = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;I2&quot;).getValue();
        var recpp = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;J2&quot;).getValue();
        var dpp = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;K2&quot;).getValue();
        var recdpp = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;L2&quot;).getValue();
        var tpp = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;M2&quot;).getValue();
        var rectpp = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;N2&quot;).getValue();
        var ship = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;O2&quot;).getValue();
        var recship = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;P2&quot;).getValue();
        var dship = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;Q2&quot;).getValue();
        var recdship = SpreadsheetApp.open(file).getSheetByName(&quot;fechas&quot;).getRange(&quot;R2&quot;).getValue();
        
  
      var fullRange = sheet2.getRange(&quot;A1:Z1001&quot;);
      fullRange.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP);
      data = [ 
       
      
        fold.getName(),
        file.getName(),
        sms,
        recsms,
        fit,
        recfit,
        dfit,
        recdfit,
        tfit,
        rectfit,
        pp,
        recpp,
        dpp,
        recdpp,
        tpp,
        rectpp,
        ship,
        recship,
        dship,
        recdship,
  
        ];
      sheet2.appendRow(data);
    }; 
  
      } catch (e) {
        // In this modification, when your folder ID cannot be used, that folder ID is skipped. At that time, an error message can be seen in the log.
        console.log(e.message);
      }
  }
  }
  </code></pre>
  <p>When I wrote the code, I added catch (e) which I thought skipped the folder if there was no excel file in that folder, but the code stops at folder &quot;QM0201&quot; which does not have an excel file in it.
  Does anybody know how I can fix it? I would like the code to run even if some folders don't have an excel files in them, those should just be skipped.
  Thank you so much in advance! Any help is appreciated.
  <a href="https://i.stack.imgur.com/z7lz8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z7lz8.png" alt="picture" /></a></p>