DriveApp.getFolders() get folders not from my Google Drive

191 views
Skip to first unread message

Heeg Ru

unread,
Feb 1, 2020, 8:13:40 AM2/1/20
to Google Apps Script Community
I write a little script to find folder "_images" on my  google drive

   var folders = DriveApp.getFolders();
 
while (folders.hasNext()) {
   
var folder = folders.next();
   
if(folder.getName()=='_images'){ idf=folder.getId();    Logger.log("folder _images if found");  break;}
 
};

it find folder "_images" not only on my drive, but all folders "_images"  I have access to (people share it for me).
how to make this script search  my folder only on my Drive, and not on drives of other people? 

jiit mahe2

unread,
Feb 1, 2020, 8:20:40 AM2/1/20
to google-apps-sc...@googlegroups.com
Hi,  
     Based on cache,  if you use userCache it can maintain individual  mail account otherwise use scriptCache the cache data deflect other shared people also. 
    Sorry for my bad english.. 

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4de2a0ab-800d-4bd5-9fca-cf13c9143a21%40googlegroups.com.

Alan Wells

unread,
Feb 1, 2020, 8:28:21 AM2/1/20
to Google Apps Script Community
You can search for folders with specific attributes.  In the example below, the search query will search for folders only owned by you with a specific title.


//See documentation at:
//https://developers.google.com/apps-script/reference/drive/drive-app#searchfoldersparams

var folders = DriveApp.searchFolders('title contains "_images" and "me" in owners');
var folder;

while (folders.hasNext()) {
  folder
= folders.next();
 
 
if(folder.getName() == '_images'){
   
    idf
= folder.getId();
   
Logger.log("folder _images if found - ID: " + idf);  
   
break;
 
}
};

Clay Smith

unread,
Feb 1, 2020, 8:30:03 AM2/1/20
to Google Apps Script Community
Get the folder’s owner. You can check if it’s the same email associated with the account. If the folder have been added to the Drive but is owned by another it will not see the folder as one you are looking for.

var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
if(folder.getName()=='_images' && folder.getOwner().getEmail==Session.getActiveUser().getEmail()){ idf=folder.getId(); Logger.log("folder _images if found"); break;}
};
Reply all
Reply to author
Forward
0 new messages