Home > front end >  Check if folder exists in EITHER parent folder, else create
Check if folder exists in EITHER parent folder, else create

Time:01-25

I have two parent folders that I want to check if Folder(folderName) exists in. folderName is defined by the range S2:2 in my spreadsheet

If Folder(folderName) does not exist in EITHER parent folder, then I want that folder to be created in Parent Folder 1.

So far my script is managing to check Parent Folder 1 and create in Parent Folder 1, but is not checking Parent Folder 2.

This is the script I have so far:

function myfunction() {
  
  var parent = DriveApp.getFolderById("parent Folder 1")
SpreadsheetApp.getActive().getSheetByName('Letter History').getRange('S2:S').getValues()
    .forEach(function (r) {
        if(r[0]) checkIfFolderExistElsesCreate(parent, r[0]);
    })
}

function checkIfFolderExistsElseCreate(parent, folderName) {
var folder;
try {
    folder = parent.getFoldersByName(folderName).next();
} catch (e) {
    folder = parent.createFolder(folderName);
}

CodePudding user response:

I believe your goal is as follows.

  • You want to expand your showing script for using 2 parent folders.

In this case, how about the following sample script?

Sample script:

Please set your parent folder IDs to parentFolderIDs and save the script.

function myfunction() {
  // Please set your parent folder IDs you want to use.
  const parentFolderIDs = ["###parent folder ID1###", "###parent folder ID2###"];

  // 1. Retrieve subfolders from folders of parentFolderIDs.
  const parentFolders = parentFolderIDs.map(id => DriveApp.getFolderById(id));
  const subFolders = parentFolders.map(folder => {
    const temp = [];
    const folders = folder.getFolders();
    while (folders.hasNext()) {
      temp.push(folders.next().getName());
    }
    return { parent: folder, subFolderNames: temp };
  });

  // 2. Create new folders when the folder names are not existing.
  const sheet = SpreadsheetApp.getActive().getSheetByName('Letter History');
  const values = sheet.getRange('S2:S'   sheet.getLastRow()).getDisplayValues();
  values.forEach(([s]) => {
    if (s) {
      subFolders.forEach(({ parent, subFolderNames }) => {
        if (!subFolderNames.includes(s)) {
          parent.createFolder(s);
        }
      });
    }
  });
}
  • When this script is run, the subfolders in parentFolderIDs are retrieved. And, using the retrieved subfolders, the new subfolders are created in the parent folders of parentFolderIDs.

References:

CodePudding user response:

function myFunction() {
  var parentFolder1 = DriveApp.getFolderById('<ParentFolder1 ID>').getFolders(); //Get ID for Folder 1
  var parentFolder2 = DriveApp.getFolderById('<ParentFolder2 ID>').getFolders(); //Get ID for Folder 2
  var sheet = SpreadsheetApp.getActive().getSheetByName('Letter History');

 //Iterating to read all the subfolders in ParentFolder 1 and 2
  var childNames = [];
  while (parentFolder1.hasNext()) {
    var child = parentFolder1.next();
    childNames.push([child.getName()]);
  }
  while (parentFolder2.hasNext()) {
    var child = parentFolder2.next();
    childNames.push([child.getName()]);
  }

  //Checking if the subfolder/s is existing or not, then creating in folder 1 if not yet existing
  var data = sheet.getRange(2, 19, sheet.getLastRow() - 1, 1).getValues(); //S = 19, Change 19 according to column. 
  for (var i = 0; i < data.length; i  ) {
    if (childNames.flat().includes(data[i][0])) {
      console.log(data[i][0]   "- already exist")
    } else {
      DriveApp.getFolderById('<ParentFolder1 ID>').createFolder(data[i][0])
      console.log(data[i][0]   "- has been created")
    }
  };
}

The above code will create subfolder/s in Folder 1 based on range S2:S column in spreadsheet, if it is checked that the subfolder name does not yet exist either in Parent Folder 1 or 2.

Please take note to change the following:

  • ParentFolder 1 ID
  • ParentFolder 2 ID

Reference: How to create a folder if it doesn't exist?

  • Related