title: How to list also files inside subfolders in google drive
body: <p>I found this google script that can list files inside a specific folder.
  When i tried it, it would only list files on the first level folder and ignore files in subfolders.</p>
  
  <p>How do i make it so it can list also everything inside all subfolders.</p>
  
  <p>This is the script</p>
  
  <pre><code>function ListNamedFilesandFolders() {
  /* Adapted from Code written by @hubgit https://gist.github.com/hubgit/3755293
  Updated since DocsList is deprecated  https://ctrlq.org/code/19854-list-files-in-google-drive-folder
  */
  
  
    // List all files and sub-folders in a single folder on Google Drive
    // declare the folder name
    var foldername = 'FolderName';
  
    // declare this sheet
    var sheet = SpreadsheetApp.getActiveSheet();
    // clear any existing contents
    sheet.clear();
    // append a header row
    sheet.appendRow(["Folder","Name", "Date Last Updated", "Size", "URL", "ID", "Description", "Type"]);
  
  
    // getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
    // folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
    var folders = DriveApp.getFoldersByName(foldername);
    var foldersnext = folders.next();
    // Logger.log("THE FOLDER IS "+foldersnext);// DEBUG
  
    // declare an array to push data into the spreadsheet
    var data = [];
  
    // list files in this folder
    // myfiles is a File Iterator
    var myfiles = foldersnext.getFiles();
  
    // Logger.log("FILES IN THIS FOLDER"); DEBUG
  
    // loop through files in this folder
    while (myfiles.hasNext()) {
      var myfile = myfiles.next();
      var fname = myfile.getName();
      var fdate = myfile.getLastUpdated(); 
      var fsize = myfile.getSize();
      var furl = myfile.getUrl();
      var fid = myfile.getId();
      var fdesc = myfile.getDescription();
      var ftype = myfile.getMimeType();
      //Logger.log("File Name is "+myfile.getName()); //Logger.log("Date is "+myfile.getLastUpdated()); //Logger.log("Size is "+myfile.getSize());
      //Logger.log("URL is "+myfile.getUrl()); //Logger.log("ID is "+myfile.getId()); //Logger.log("Description is "+myfile.getDescription());
      //Logger.log("File Type is "+myfile.getMimeType());
  
      // Populate the array for this file
      data = [ 
        foldersnext,
        fname,
        fdate,
        fsize,
        furl,
        fid,
        fdesc,
        ftype
      ];
      //Logger.log("data = "+data); //DEBUG
      sheet.appendRow(data);
    } // Completes listing of the files in the named folder
  
    // Now get the subfolder
    // subfolders is a Folder Iterator
    var subfolders = foldersnext.getFolders();
    //Logger.log("THE SUBFOLDER(S) ARE"); DEBUG HEADING
  
    // now start a loop on the SubFolder list
    while (subfolders.hasNext()) {
      var subfolderdata = [];
      var mysubfolders = subfolders.next();
      var mysubfolder = mysubfolders.getName();  
      //Logger.log("Subfolder name:"+mysubfolder); //DEBUG
  
      // Get the files
      // mysubfiles is a File Iterator
      var mysubfiles = mysubfolders.getFiles();
      //Logger.log("FILES IN THIS FOLDER"); //DEBUG HEADING
  
      // now start a loop on the files in the subfolder
      while (mysubfiles.hasNext()) {
        var smyfile = mysubfiles.next();
        var sfname =  smyfile.getName();
        var sfdate =  smyfile.getLastUpdated(); 
        var sfsize =  smyfile.getSize();
        var sfurl =  smyfile.getUrl();
        var sfid =  smyfile.getId();
        var sfdesc =  smyfile.getDescription();
        var sftype =  smyfile.getMimeType();
        //Logger.log("Subfolder is "+foldersnext+"/"+mysubfolder); 
        //Logger.log("File Name is "+ smyfile.getName()); Logger.log("Date is "+ smyfile.getLastUpdated()); Logger.log("Size is "+ smyfile.getSize());
        //Logger.log("URL is "+ smyfile.getUrl()); Logger.log("ID is "+ smyfile.getId()); Logger.log("Description is "+ smyfile.getDescription());Logger.log("File Type is "+ smyfile.getMimeType());
        subfolderdata = [ 
          (foldersnext+"/"+mysubfolder),
          sfname,
          sfdate,
          sfsize,
          sfurl,
          sfid,
          sfdesc,
          sftype
        ];
        //Logger.log("subfolderdata = "+subfolderdata);
        sheet.appendRow(subfolderdata);
      }
    }
  }
  </code></pre>
  
  <p>My post wouldn't go trough because my post is mostly code and i need to post "more details". 
  But there is no more details to add really.</p>
  
  <p>So instead of drawing unicorns all over the post i'm gonna do this.</p>
  
  <p><strong>Ignore below</strong></p>
  
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vel quam elementum pulvinar etiam non quam. Eu mi bibendum neque egestas. Tempor nec feugiat nisl pretium. Vulputate enim nulla aliquet porttitor. In dictum non consectetur a erat nam at lectus urna. Suspendisse sed nisi lacus sed viverra tellus in hac. Ipsum consequat nisl vel pretium lectus quam id. Sit amet tellus cras adipiscing enim eu. Sapien et ligula ullamcorper malesuada. Quis vel eros donec ac. Aenean sed adipiscing diam donec adipiscing tristique risus nec. Quis eleifend quam adipiscing vitae proin sagittis nisl. Nunc eget lorem dolor sed viverra ipsum nunc aliquet.</p>
  
  <p>Eget felis eget nunc lobortis mattis aliquam faucibus. Gravida in fermentum et sollicitudin. Nisl purus in mollis nunc sed. Diam volutpat commodo sed egestas egestas fringilla phasellus. Nunc sed velit dignissim sodales. Egestas integer eget aliquet nibh praesent tristique. Sit amet cursus sit amet dictum sit. Et ligula ullamcorper malesuada proin libero nunc. Suspendisse potenti nullam ac tortor vitae purus faucibus ornare. Condimentum lacinia quis vel eros donec ac odio tempor orci. Aliquam eleifend mi in nulla posuere sollicitudin. Dictum at tempor commodo ullamcorper a lacus vestibulum. Placerat orci nulla pellentesque dignissim. Montes nascetur ridiculus mus mauris vitae. Lacus suspendisse faucibus interdum posuere lorem ipsum dolor. Maecenas pharetra convallis posuere morbi leo urna molestie at elementum. Massa ultricies mi quis hendrerit dolor magna. Et sollicitudin ac orci phasellus. Ut placerat orci nulla pellentesque dignissim enim sit amet venenatis.</p>