title: How to filter by file path in Google apps script
body: <p>I am trying to have the following script filter by file path. Ideally this should only show results from the folder marked 'GUEST' in my drive. Right now it shows those and anything else with the shared folder ID of this root (I do not want to use the GUEST folder ID because I will later use this to filter other users accessing my drive). </p>
  
  <p>My google drive file path is as follows <code>Root/GUEST</code></p>
  
  <p>CODE UPDATED WITH ANSWER FROM COMMENTS:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var folderId = "MyID"; // &lt;--- Your shared folder ID
  
  function doGet() {
      var t = HtmlService.createTemplateFromFile('index');
      t.data = getFileList();
      return t.evaluate() .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);;
  
  
  }
  
  
  
  
  function getparams(e) {
      return zipping(typeof(e.fileId) == "string" ? [e.fileId] : e.fileId);
  }
  
  function getFileList() {
      var folderlist = (function(folder, folderSt, results) {
          var ar = [];
          var folders = folder.getFoldersByName("GUEST");
          while (folders.hasNext()) ar.push(folders.next());
          folderSt += folder.getId() + "#_aabbccddee_#";
          var array_folderSt = folderSt.split("#_aabbccddee_#");
          array_folderSt.pop()
          results.push(array_folderSt);
          ar.length == 0 &amp;&amp; (folderSt = "");
          for (var i in ar) arguments.callee(ar[i], folderSt, results);
          return results;
      })(DriveApp.getFoldersByName("GUEST").next(), "", []);
      var localTimeZone = Session.getScriptTimeZone();
      var filelist = [];
      var temp = {};
      for (var i in folderlist) {
          var folderid = folderlist[i][folderlist[i].length - 1];
          var folder = DriveApp.getFoldersByName("GUEST");
          var files = folder.next().getFiles();
          while (files.hasNext()) {
              var file = files.next();
              temp = {
                 folder_tree: function(folderlist, i) {
                    
                      if (i &gt; 0) {
                          return "/" + [DriveApp.getFolderById(folderlist[i][j]).getName() for (j in folderlist[i])
                              if (j &gt; 0)].join("/") + "/";
                      } else {
                          return "/";
                      }
                  }(folderlist, i),
                  file_id: file.getId(),
                  file_name: file.getName(),
                  file_size: file.getBlob().getBytes().length,
                  file_created: Utilities.formatDate(file.getDateCreated(), localTimeZone, "yyyy/MM/dd HH:mm:ss"),
                  file_updated: Utilities.formatDate(file.getLastUpdated(), localTimeZone, "yyyy/MM/dd HH:mm:ss"),
              };
              filelist.push(temp);
              temp = {}
          }
      }
     
  var sortedlist = filelist.sort(function(e1, e2) {
          return (e1.folder_tree &gt; e2.folder_tree ? 1 : -1) });
    
  
      return sortedlist;  
  
  }
  
  
  
  
  function zipping(fileId) {
      var blobs = [];
      var mimeInf = [];
      fileId.forEach(function(e) {
          try {
              var file = DriveApp.getFileById(e);
              var mime = file.getMimeType();
              var name = file.getName();
          } catch (e) {
              return e
          }
          Logger.log(mime)
          var blob;
          if (mime.indexOf('google-apps') &gt; 0) {
              mimeInf =
                  mime == "application/vnd.google-apps.spreadsheet" ? ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name + ".xlsx"] : mime == "application/vnd.google-apps.document" ? ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", name + ".docx"] : mime == "application/vnd.google-apps.presentation" ? ["application/vnd.openxmlformats-officedocument.presentationml.presentation", name + ".pptx"] : ["application/pdf", name + ".pdf"];
              blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "/export?mimeType=" + mimeInf[0], {
                  method: "GET",
                  headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() },
                  muteHttpExceptions: true
              }).getBlob().setName(mimeInf[1]);
          } else {
              blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "?alt=media", {
                  method: "GET",
                  headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() },
                  muteHttpExceptions: true
              }).getBlob().setName(name);
          }
          blobs.push(blob);
      });
      var zip = Utilities.zip(blobs, Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyyMMdd_HHmmss") + '.zip');
      var bytedat = DriveApp.createFile(zip).getBlob().getBytes();
      return Utilities.base64Encode(bytedat);
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
     function postLogin(event) {
          var form = document.getElementById("myForm");
          form.submit();
  
          event.preventDefault();
      }
  &lt;/script&gt; 
   &lt;a href="" onClick="postLogin(event);" &gt;Click!&lt;/a&gt;
   &lt;form id="myForm" action="MYEXECLINK" target="my_iframe"&gt;&lt;/form&gt;
   &lt;iframe id="my_iframe"name="my_iframe"  style= "width: 500px; height: 50%;" frameBorder="0" style="overflow:hidden"&gt;&lt;/iframe&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I tried to use an if statement at line 61, but it could not pull the variables for some reason (I think I'm not setting up my variables correctly):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  if (filelist(folder_tree == "/GUEST/"){
      return sortedlist;}
    else
    {return null}</code></pre>
  </div>
  </div>
  </p>
  
  <p>Does anyone know how to make this script filter by folder_tree? Hopefully by comparing it to a var (like user=guest)?</p>
  
  <p>To clarify, here is the current result:</p>
  
  <p><a href="https://i.stack.imgur.com/oSxOf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oSxOf.png" alt="enter image description here"></a></p>
  
  <p>And this is the expected output:</p>
  
  <p><a href="https://i.stack.imgur.com/nxLpV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nxLpV.png" alt="enter image description here"></a></p>
  
  <p>Thanks for any help, I'll post updates as I work on it to clarify. </p>