Seth
Seth

Reputation: 123

DriveApp: Get File ID with Parent Folder ID/Name and File Name

I'm trying to use DriveApp to determine a file ID utilizing:

I thought I'd use this: https://yagisanatode.com/2018/10/05/google-apps-script-get-file-by-name-with-optional-parent-folder-crosscheck/

But it always returns:

There are multiple files named: (filename). But none of them are in folder, (foldername).

...which is not true. Any ideas or any easier way to do this?

Thanks in advance.

Here's a minimal example:

/*
* ****Get File By Name***
*
*param 1: File Name 
*param 2: Parent Folder of File (optional)
*
*returns: Dictionary of file "id" and "error" message {"id": ,"error": }
*  -if there is no error, "id" returns file id and "error" returns false
*  -if there is an error, "id" returns false and "error" returns type of error as a string for user to display or log.
*/

function getFileByName(fileName, fileInFolder){
 
  var filecount = 0;
  var dupFileArray = [];
  var folderID = "";
  
  var files = DriveApp.getFilesByName(fileName);
  
  while(files.hasNext()){
    var file = files.next();
    dupFileArray.push(file.getId());
    
    filecount++;
  };
  
  if(filecount > 1){
    if(typeof fileInFolder === 'undefined'){
        folderID = {"id":false,"error":"More than one file with name: "+fileName+". \nTry adding the file's folder name as a reference in Argument 2 of this function."}
    
    }else{
     //iterate through list of files with the same name
     for(fl = 0; fl < dupFileArray.length; fl++){
       var activeFile = DriveApp.getFileById(dupFileArray[fl]);
       var folders = activeFile.getParents();
       var folder = ""
       var foldercount = 0;
       
       //Get the folder name for each file
       while(folders.hasNext()){
         folder = folders.next().getName(); 
         foldercount++;
       };
       
       if(folder === fileInFolder && foldercount > 1){
         folderID = {"id":false,"error":"There is more than one parent folder: "+fileInFolder+" for file "+fileName}
       };
       
       if(folder === fileInFolder){
           folderID = {"id":dupFileArray[fl],"error":false};
           
       }else{
         folderID = {"id":false,"error":"There are multiple files named: "+fileName+". \nBut none of them are in folder, "+fileInFolder}
       };
     };
   };
   
  }else if(filecount === 0){
      folderID = {"id":false,"error":"No file in your drive exists with name: "+fileName};
      
  }else{ //IF there is only 1 file with fileName
    folderID = {"id":dupFileArray[0],"error":false};
    };
 
  return folderID;
};

function test() {
//My arguments for the function
var myFileName = "Invoices";
var myFileParentFolderName = "testing";
 
//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);
};
}

Upvotes: 2

Views: 3984

Answers (1)

Marios
Marios

Reputation: 27390

The solution is much simpler than you think.

  • In the following script, adjust the folderID and fileName to your particular case and it will store in an array all the IDs if multiple files exist in this folder with the selected name.
  • If the file exists only one, then you will get an array of a single element.

Solution:

function findFilesInfo() {

  const folderId = 'folderID';
  const fileName = 'fileName';
  
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFilesByName(fileName);
  const fileIDs = [];

  while (files.hasNext()) {
      var file = files.next();
      fileIDs.push(file.getId());
  }

  Logger.log(fileIDs);  
}

fileIDs has the list of IDs of the particular fileName under the folder with ID folderID.

Upvotes: 6

Related Questions