title: Google Form Upload to New Folder
body: <p>I have a form with a number of questions including first name, last name and several questions that require the user to upload a document. My goal is that when the user submits the form that a new folder will be created, a suffix added if there is already a folder with the same name and all of the files uploaded go into the new folder for that user. Thanks to the solution posted by Tanaike “<a href="https://stackoverflow.com/questions/64531195/google-form-upload-files-to-specific-new-folder-based-on-the-value-submitted">here</a>” and with more modifications from Tanaike &quot;<a href="https://stackoverflow.com/questions/64565806/how-to-automatically-rename-folder-based-on-uploaded-file-if-there-are-duplicate">here</a>&quot;, the form works in completing the new folder however there are 2 elements I’m trying to figure out;</p>
  <ol>
  <li>How do I move all uploads from the submission? Currently only the first upload question moves to the new folder.</li>
  <li>Our folder names are usually, “lastname, firstname”. I can change the form for the user to enter their name like this but it seems like figuring out how to combine the name fields would be the better solution ie. Last Name+”, “+First Name</li>
  </ol>
  <pre class="lang-js prettyprint-override"><code>function onFormSubmit1(e) {
    const folderId = &quot;1fOttSZooEA-dK8ee7COzRtrjVQhzh2Yv&quot;;  // Please set top folder ID of the destination folders.
    const form = FormApp.getActiveForm();
    const formResponses = form.getResponses();
    const itemResponses = formResponses[formResponses.length-1].getItemResponses();
  
    Utilities.sleep(3000); // This line might not be required.
  
  // Prepare the folder.
  const destFolder = DriveApp.getFolderById(folderId);
  let folderName = itemResponses[0].getResponse();
  const subFolder = destFolder.searchFolders(&quot;title contains '&quot; + folderName + &quot;'&quot;);
  const reg = new RegExp(`^${folderName}$|^${folderName}_\\d{2}$`);
  if (subFolder.hasNext()) {
    const folderNames = {};
    while (subFolder.hasNext()) {
      const fol = subFolder.next();
      const fName = fol.getName();
      if (reg.test(fName)) folderNames[fName] = fol;
    }
    const len = Object.keys(folderNames).length;
    if (len == 1 &amp;&amp; folderNames[folderName]) {
      folderNames[folderName].setName(folderName + &quot;_01&quot;);
    }
    folderName += &quot;_&quot; + (&quot;00&quot; + (len + 1)).slice(-2);
  }
  const folder = destFolder.createFolder(folderName);
  
    // Move files to the folder.
    itemResponses[1].getResponse().forEach(id =&gt; DriveApp.getFileById(id).moveTo(folder));
  }
  </code></pre>