title: DriveApp: Get File ID with Parent Folder ID/Name and File Name
body: <p>I'm trying to use DriveApp to determine a file ID utilizing:</p>
  <ul>
  <li>Either the parent folder name, or the parent folder ID</li>
  <li>The file name in question</li>
  </ul>
  <p>I thought I'd use this:
  <a href="https://yagisanatode.com/2018/10/05/google-apps-script-get-file-by-name-with-optional-parent-folder-crosscheck/" rel="nofollow noreferrer">https://yagisanatode.com/2018/10/05/google-apps-script-get-file-by-name-with-optional-parent-folder-crosscheck/</a></p>
  <p>But it always returns:</p>
  <blockquote>
  <p>There are multiple files named: (filename). But none of them are in folder, (foldername).</p>
  </blockquote>
  <p>...which is not true. Any ideas or any easier way to do this?</p>
  <p>Thanks in advance.</p>
  <p>Here's a minimal example:</p>
  <pre><code>/*
  * ****Get File By Name***
  *
  *param 1: File Name 
  *param 2: Parent Folder of File (optional)
  *
  *returns: Dictionary of file &quot;id&quot; and &quot;error&quot; message {&quot;id&quot;: ,&quot;error&quot;: }
  *  -if there is no error, &quot;id&quot; returns file id and &quot;error&quot; returns false
  *  -if there is an error, &quot;id&quot; returns false and &quot;error&quot; returns type of error as a string for user to display or log.
  */
  
  function getFileByName(fileName, fileInFolder){
   
    var filecount = 0;
    var dupFileArray = [];
    var folderID = &quot;&quot;;
    
    var files = DriveApp.getFilesByName(fileName);
    
    while(files.hasNext()){
      var file = files.next();
      dupFileArray.push(file.getId());
      
      filecount++;
    };
    
    if(filecount &gt; 1){
      if(typeof fileInFolder === 'undefined'){
          folderID = {&quot;id&quot;:false,&quot;error&quot;:&quot;More than one file with name: &quot;+fileName+&quot;. \nTry adding the file's folder name as a reference in Argument 2 of this function.&quot;}
      
      }else{
       //iterate through list of files with the same name
       for(fl = 0; fl &lt; dupFileArray.length; fl++){
         var activeFile = DriveApp.getFileById(dupFileArray[fl]);
         var folders = activeFile.getParents();
         var folder = &quot;&quot;
         var foldercount = 0;
         
         //Get the folder name for each file
         while(folders.hasNext()){
           folder = folders.next().getName(); 
           foldercount++;
         };
         
         if(folder === fileInFolder &amp;&amp; foldercount &gt; 1){
           folderID = {&quot;id&quot;:false,&quot;error&quot;:&quot;There is more than one parent folder: &quot;+fileInFolder+&quot; for file &quot;+fileName}
         };
         
         if(folder === fileInFolder){
             folderID = {&quot;id&quot;:dupFileArray[fl],&quot;error&quot;:false};
             
         }else{
           folderID = {&quot;id&quot;:false,&quot;error&quot;:&quot;There are multiple files named: &quot;+fileName+&quot;. \nBut none of them are in folder, &quot;+fileInFolder}
         };
       };
     };
     
    }else if(filecount === 0){
        folderID = {&quot;id&quot;:false,&quot;error&quot;:&quot;No file in your drive exists with name: &quot;+fileName};
        
    }else{ //IF there is only 1 file with fileName
      folderID = {&quot;id&quot;:dupFileArray[0],&quot;error&quot;:false};
      };
   
    return folderID;
  };
  
  function test() {
  //My arguments for the function
  var myFileName = &quot;Invoices&quot;;
  var myFileParentFolderName = &quot;testing&quot;;
   
  //Run the function
  var getFileID = getFileByName(myFileName, myFileParentFolderName);
   
  //Check if folder exists
  if(getFileID.id === false){ //if file cannot be accurately found.
    Logger.log(getFileID.error); //alert or log error. Give option to try another FileName
  }else{
    // If the file ID exists then proceed with the program. 
    Logger.log(getFileID.id);
  };
  }
  </code></pre>