0

I am creating a script to add a folder on all subfolders if the folder is not existing. But unfortunately, I am generating only on one folder. How to make this changes under a parentfolder? Thank you for your answers.

Here is my code:

function folder_create(){

var foldername = 'CHECK FOLDS';
var folders = DriveApp.getFoldersByName(foldername);
var foldersnext = folders.next();
var subfolders = foldersnext.getFolders();

var subfolders = subfolders.next();
var files = subfolders.searchFolders("title contains 'Testing Test'");
{
while(files.hasNext()){
var reportFolderExist = files.next();
var yearFolders = reportFolderExist.getFoldersByName("TESTTT");
if(yearFolders.hasNext()){
   
} else {
    reportFolderExist.createFolder("testing only----");
    
}
}  

}}
Tan Al
  • 3
  • 1
  • 1
    Does this answer your question? [Create a new folder in Drive using Google App Script](https://stackoverflow.com/questions/41648174/create-a-new-folder-in-drive-using-google-app-script) – joshmeranda Mar 11 '21 at 03:04

1 Answers1

0

Current situation:

If I understand you correctly, this is your current situation:

  • You have a main folder called CHECK FOLDS.
  • This main folder contains several subfolders.
  • For each of these subfolders, you want to check whether they contain a folder whose title contains Testing Test.
  • If the subfolder doesn't contain a Testing Test folder, create it.

Issue and solution:

If this is correct, the main issue in your code is that you are not iterating through all subfolders, but only accessing the first one var subfolders = subfolders.next();. You should use a loop instead, in order to check each subfolder (see code sample below).

Also, your code contains some confusing parts, like iterating through files (that is Testing Test folders) and looking for folders called TESTTT inside of these files. I don't think this makes sense, if the current situation is the one I described above.

Code sample:

function folder_create(){
  var foldername = 'CHECK FOLDS';
  var folders = DriveApp.getFoldersByName(foldername);
  var foldersnext = folders.next(); // Main folder
  var subfolders = foldersnext.getFolders();
  while (subfolders.hasNext()) { // Loop through subfolders
    var subfolder = subfolders.next(); // Subfolder
    var files = subfolder.searchFolders("title contains 'Testing Test'");
    if (!files.hasNext()) { // Check if "Testing Test" exists.
      subfolder.createFolder("Testing Test"); // Create "Testing Test" folder inside subfolder
    }
  }
}
Iamblichus
  • 18,540
  • 2
  • 11
  • 27