Create a file with the same name as the folder where a file was uploaded

I have an HTML form within a Web App, created with GAS.

This HTML form was created from this file upload script here:


This is the HTML form:

enter image description here

The point is that I needed the files to be uploaded in folders that follow the following pattern:
enter image description here

The first number refers to the model selected on the form, the second number refers to the slot used.

Therefore, it was necessary to create a function to identify the input chosen in the Model and, according to this input, check which is the first empty folder, then take the ID of that folder and pass it to the client side to upload the file inside it.

With the help of some members of the community, some adaptations were made and the final function was this:

    /** Modified version of script written by Tanaike */
    function createOrGetFolder(folderName, parentFolderId) {
      try {
        var parentFolder = DriveApp.getFolderById(parentFolderId), folder;
        if (parentFolder) {
          var foldersIter = parentFolder.getFoldersByName("Video");
          if (foldersIter.hasNext()) {
            var videoFolder =;
            var nextFolderName = folderName + "-01";
            while (!folder) {
              video_folder = videoFolder.getFoldersByName(nextFolderName);
              if (video_folder.hasNext()) {
                folder =;
                var files = folder.getFiles();
                if (files.hasNext()) {
                  var [a, b] = nextFolderName.split("-");
                  nextFolderName = `${a}-${String(Number(b) + 1).padStart(2, "0")}`;
                  folder = null;
              } else {
                folder = videoFolder.createFolder(nextFolderName);
          } else {
            folder = parentFolder.createFolder("Video");
            folder = folder.createFolder(folderName);
        } else {
          throw new Error("Parent Folder with id: " + parentFolderId + " not found");
        return folder.getId();
      } catch (error) {
        return error;

It works perfectly, the point is that this form also has a function that generates a .csv file when the form is submitted, the function is this one:

.gs file:

const saveDataAsCSV = (data, folderId) => DriveApp.getFolderById(folderId).createFile("Sample.csv", data);

HTML file:

var name = $('#name01').val();
      var description = $('#description').val();
      var model = $('#Model').val();
      upload_folder = model;
      var color = $('#Color').val();
      var form_values = [name, description, model, color];
      var data = form_values.join(",");, uploadParentFolderId);

My goal is to make the .csv file be generated with the same name as the folder where the file was uploaded, that is, if the file is uploaded in folder 01-01, the file name has to be 01-01.csv, if the file is uploaded in the 02-02 folder, the file name has to be 02-02.csv, and so on.

How can I achieve this?

The complete script can be viewed in the GAS of this worksheet