I've been playing around with google drive scripts.
I need to create a folder from a spreadsheet in case a folder with that name doesn't exist.
The problem though, is that it says the folder exists even if it doesn't.
The check is inside a try
function .
Like this :
try {
//currentName is the name of the folder and it's working properly
var folder = DriveApp.getFoldersByName(currentName);
Logger.log('That folder already exists!')
} catch(err) {
var folder = DriveApp.createFolder(currentName);
Logger.log('Folder created because it didnt exist');
}
Any reason why it always finds a folder even if it doesn't exist ?
It worked fine on top of my drive. But then I placed it inside a server drive that has a lot of folders. Is it a permissions problem? Or I'm not retrieving the current folders in the server?
Am I missing any type of checks?
I've been playing around with google drive scripts.
I need to create a folder from a spreadsheet in case a folder with that name doesn't exist.
The problem though, is that it says the folder exists even if it doesn't.
The check is inside a try
function .
Like this :
try {
//currentName is the name of the folder and it's working properly
var folder = DriveApp.getFoldersByName(currentName);
Logger.log('That folder already exists!')
} catch(err) {
var folder = DriveApp.createFolder(currentName);
Logger.log('Folder created because it didnt exist');
}
Any reason why it always finds a folder even if it doesn't exist ?
It worked fine on top of my drive. But then I placed it inside a server drive that has a lot of folders. Is it a permissions problem? Or I'm not retrieving the current folders in the server?
Am I missing any type of checks?
Share Improve this question edited Apr 21, 2015 at 10:36 Joel Almeida asked Apr 21, 2015 at 10:23 Joel AlmeidaJoel Almeida 8,0475 gold badges27 silver badges51 bronze badges 1- Doesnt return "true" or a folder. See the docs. – Zig Mandel Commented Apr 21, 2015 at 13:24
1 Answer
Reset to default 8Find out the problem.
DriveApp.getFoldersByName
returns a FolderIterator
, so I must do an extra test after that.. Like this :
var folder = DriveApp.getFoldersByName(currentClient);
if(folder.hasNext()) {
Logger.log('Folder already exists')
} else {
var folder = DriveApp.createFolder(currentClient);
Logger.log('New folder created!');
}
It worked perfectly like this.