Home > database >  How to check for file existence in the "google drive folder" and download the file in &quo
How to check for file existence in the "google drive folder" and download the file in &quo

Time:01-14

I'm trying to add a code in my exisiting script to pave its way for finding the existing files in the selected Google drive for by getFolderbyId command, what so ever the file is of any kind the program should check and upon having files on Google Drive folder it would popup a message "There are already files stored in this folder, would you like to continue anyway?" then upon pressing yes button the files by url list would be downloaded to the Google drive folder.

I've made this script by DriveApp.getFolderById(folder_id); command, so Please it is requested to edit or suggest code with this ById command. I'd also be very much glad if you guys could fix the "task has been completed" syntax as it is not working after the completion of all the loops and the after the whole function.

Code is given below:

    function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
    .createMenu('Custom Menu')
    .addItem('Show alert', 'showAlert')
    .addToUi();
}

function showAlert() {
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.alert(
    Please confirm ',
    Are you sure you want to
    continue ?
        if so then please make sure that your drive folder is completely empty ',
    ui.ButtonSet.YES_NO);
// Process the user's response.
if (result == ui.Button.YES) {
    // User clicked "Yes".
    {
        let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        let lastRow = sheet.getLastRow();
        for (let i = 0; i < lastRow - 2; i  ) {
            const ss = SpreadsheetApp.getActive();
            const folder_id = ss.getRange("Sheet1!J4").getValue(); // get value in "Settings!C9"
            const destinationFolder = DriveApp.getFolderById(folder_id)

            let folder = DriveApp.getFolderById(folder_id);
            let url = sheet.getRange(3   i, 1).getValue();
            let image = SpreadsheetApp.newCellImage().setSourceUrl(url);
            let blob = UrlFetchApp.fetch(url).getBlob();
            let name = sheet.getRange(3   i, 2).getValue();
            folder.createFile(blob).setName(name);
            sheet.getRange(3   i, 3).setValue("Completed");
        }
        ui.alert("task has been completed")

    };

} else {
    // User clicked "No" or X in the title bar.
    ui.alert('Permission denied.');
}
}

CodePudding user response:

The following example will list all the files with in a folder. You can then test on say the file name and mime type to detect if the file already exists.

function main(){

  var name = "Untitled spreadsheet"
  var type = "application/vnd.google-apps.spreadsheet"
  var folderId = '1bUTP9uKOpPpsn-UrOH0QEF6v2g3qhMGx'

var exists = FileExists(folderId, name, type);

if(exists){
  console.log("File Name exits of this mimeType")
}else {
  console.log("File Name does not exist of this mimeType")
}

}


function FileExists(folderId, name, mimeType){  
 
  var folder = DriveApp.getFolderById(folderId); 
  
  var files = folder.getFiles();
  while (files.hasNext()){
    file = files.next();
    
    console.log(file.getName(),file.getId(),file.getMimeType())

    if(file.getName() == name && file.getMimeType()== mimeType ){
      return true;
    }    
  } 

  return false;
}
  • Related