I'm trying to put attachments in my drive folders automatically. The script/loop runs fine until there are similar foldernames to look for. The code only gives the first one. So when i have two drive folders one named "Test" en the other named "Test new" and the input for 'driveFolder' in de code is "Test new" it puts the attachment in the folder "Test". Can anyone help me with getting the folder with the exact foldername? (The input for 'driveFolder is coming from a sheet with all the foldernames) Below is the (part of) the code i use:
var folders = DriveApp.getFoldersByName(driveFolder);
var folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folder_name);
Thanks in advance
Folders' name in GDrive don't have to be unique, thus trying to uniquely identify folders by names has to fail.
Folders IDs are unique, so you can be sure you are saving the exact folder you want to save.
DocsList.getFolderById("123412341234")
On a side note, little knowing about data structure Google Drive uses, it definitely does not look like a tree. More like a hashmap.
--Edit --
Google Workspace Reference:
DriveApp.getFolderById(id)
DriveApp.getFoldersByName(name)
*Note getting folders by name returns a collection in a FolderIterator, as @matcheek describes. You could get all folders by that name and then choose the right one by some other criteria, or even use all of them if needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With