I'm not sure if I understood well what is your goal. But if you want to create folder 'FirstnameLastname' within some specific folder, you should first select this 'parent' folder and then use it for creating new one.
EDIT:
Summary
First look at the reference from Google. You use class DriveApp
and methode .createFolder()
. Google cheat sheet says, that this methode within the DriveApp class 'Creates a folder in the root of the user's Drive with the given name.'. This methode returns folder
as return type (your variable type will be folder).
This is the reason why your code creates folders only in root folder.
Look in the cheatsheet again but now look at the folder
class. You can use the same methode .createFolder()
with this class. Result is following: 'Creates a folder in the current folder with the given name.' This methode returns again folder
.
Solution
Google Drive:
- Create (or open) manually your desired folder in your Google Drive repository.
- Find the ID of your folder (when you open the folder, you can find the id at the end of the URL:
drive.google.com/drive/folders/{this is your ID}
)
Script:
- Get your desired destination folder
var parentFolder = DriveApp.getFolderById("{put your folder id here}");
- Add your code
- When you are creating new folder within parent folder, just call the
.createFolder()
methode from your parentFolder
which you have created earlier: var folder = parentFolder.createFolder();
Edited script:
function uploadFiles(form) {
try {
var dropbox = form.FirstName + form.LastName;
var parentFolder = DriveApp.getFolderById('parentfolderId'); //add this line...
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = parentFolder.createFolder(dropbox); //edit this line
}
var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.Name);
return "Thank you, your file was uploaded successfully!";
}