List of drive files/folders under specific folder owned by a user

7,174 views
Skip to first unread message

Manuel Serrenti

unread,
Jan 5, 2016, 2:21:11 PM1/5/16
to Google Apps Manager
Hi all, and thanks to the developers for this GREAT tool. I was wondering if there's a method to obtain a list of drive files/folders (with ids) owned by a specific user under a specific folder.

The use case is:
A shared folder owned by a "generic" user in organization (something like drive@domain) is updated with files and folders by other users in the same organization; if a user leave the organization, the files he shared in that folder must be transfered to the "generic user".

I know it's possible to batch the ownership change with " GAM user update drivefileacl ..." command, but i need a CSV with the id's of the files/folders.

Any advice?

Many thanks
Manuel

Ross Scroggs

unread,
Jan 5, 2016, 11:44:01 PM1/5/16
to google-ap...@googlegroups.com
Manuel,

You can issue a query as described here: https://developers.google.com/drive/v2/web/search-parameters
Assumptions: generic user is gen...@domain.com; departed user is departed@domain,com, shared folder title is 'Shared Files'
First, we need the id of the shared folder: gam user gen...@domain.com show filelist query 'title="Shared Files"' id
This should give one a one record CSV file, copy the value in the id column, let's call it 'abcddhddsdisididuid'
Now we want the files owned by departed in that folder: gam user depa...@domain.com show filelist query ''"abcddhddsdisididuid" in parents' id
That should produce A CSV file with a column of the file ids owned by departed in 'Shared Files'
The queries are surrounded by ' '; any text in the queries is surrounded by " ".

Ross
--
ross.s...@gmail.com
> --
> You received this message because you are subscribed to the Google Groups "Google Apps Manager" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
> To post to this group, send email to google-ap...@googlegroups.com.
> Visit this group at https://groups.google.com/group/google-apps-manager.
> To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/b1b41dea-d363-41f0-a71d-c705a55f7b52%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Manuel Serrenti

unread,
Jan 6, 2016, 6:38:54 AM1/6/16
to Google Apps Manager
First of all, thanks for the answer :)
I've tried with the query you provided; it works, but it gives me only the files/folders in the specific folder, not recursively
Making further searches in stackoverflow led me to an interesting thing i didn't noticed before:
the ID of files/folders is a combination of the parent ID+file ID, so if I search for ALL files owned by a user, then (locally) filter for relevant string in ID, I can identify the desired files.

It would be nice to have a command to make a recursive search in a folder and returns all the IDs for a user, so it can be passed to another command in GAM to change owner in batch.
And, if possibile, add the "path" column to the "filelist" options, so it would be much more simple to track down specific files in specific folders making a simple query in the google spreadsheet ;)
i known that a similar thing is possibile via Google Script, but i don't want to add google script in this workflow because i want it simple and fast.

Side note: using query in GAM on Windows with ' ' surrounding the query returns an error; i've just used " " to surround the query command and '  ' to surround the search term inside the query
example: gam user john@domain show filelist query "title = 'shared folder'" id

Again, thanks for the support!

Manuel Serrenti

unread,
Jan 6, 2016, 9:00:54 AM1/6/16
to Google Apps Manager
I'm adding some useful things discovered so far (maybe I'm doing it wrong, so make your own tests):

i'm trying to obtain the list of files owned by a specific user in another user's drive, but without success, with this command:
gam user general_user@domain show filelist query "'other_user@domain' in owners"

it returns zero results... It's strange, because if i use the regular Drive GUI, i can find all files in My Drive owned by other users... I'm doing it wrong? 

if I change "owners" and put "writers", obviously it returns something, but it's not what I need to track down files owned by other users in "general_user"'s Drive...

Ross Scroggs

unread,
Jan 6, 2016, 9:36:14 AM1/6/16
to google-ap...@googlegroups.com
Manuel,

gam user general_user@domain show filelist query "'other_user@domain' in owners"

This doesn't work because gam initializes the query parameter to  '"me" in owners' and then appends your query. This leaves you with:
gam user general_user@domain show filelist query "'me' in owners and 'other_user@domain' in owners"

--
You received this message because you are subscribed to the Google Groups "Google Apps Manager" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Manuel Serrenti

unread,
Jan 6, 2016, 10:23:49 AM1/6/16
to Google Apps Manager
Thanks again Ross, you're very kind :)

I've tested another command:

gam user general_user@domain show filelist query "not 'me' in owners or 'other_user@domain' in owners" allfields todrive

and it returned a list of files owned by "other_user" in "general_user"'s Drive.

using allfields options is useful (the columns "userPermission.role", "userPermission.type" and "userPermission.id" are very useful to make a double check)

Can you confirm that this command is correct?

IMHO it's a good thing to consider to add a new command (or a logic rewrite of the actual command) to perform Drive searches with arbitrary queries? something like

gam show filelist query "put query here" ? (without the user at the beginning)

Thanks to you and Jay Lee for this invaluable tool!

Ross Scroggs

unread,
Jan 6, 2016, 10:58:43 AM1/6/16
to google-ap...@googlegroups.com
Manuel,

Clever idea.
Your command: gam user general_user@domain show filelist query "not 'me' in owners or 'other_user@domain' in owners" allfields todrive
became: gam user general_user@domain show filelist query "'me' in owners and not 'me' in owners or 'other_user@domain' in owners" allfields todrive
Yielding exactly what you want becase the first two clauses cancel out.

It would be easy to add a new argument (fullquery <Query>) that let you specify a full query while not breaking any existing scripts that expect the current behavior with query.

--
You received this message because you are subscribed to the Google Groups "Google Apps Manager" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Manuel Serrenti

unread,
Jan 7, 2016, 12:21:23 PM1/7/16
to Google Apps Manager
A little recap, maybe is useful for other users:

the command 
gam user USER1 show filelist query "not 'me' in owners or 'USER2' in owners" allfields todrive
returns a list of all files and folders owned by USER2 that is stored in USER1 Drive.

So, given that list of ID's, is simple to change the owner of that files with the command
gam user USER2 update drivefileacl FILEID USER1 role owner transferownership true
using powershell, bash scripting or CSV capabilities in GAM (the choice is yours!).

This is useful in organization that use a general user to store and share files for the entire organization, and want to have more control over permissions (and avoid accidental deletes/orphaned files).
If the script is triggered every day (or every hour...again, the choice is yours) is a huge help to prevent data loss.

Just to clarify: actually I haven't found a command that can track down files owned by a user INSIDE a specific folder; the command above find files owned by USER2 inside the entire USER1 Drive.
In a previous post I've said that the file ID contains the Folder ID + File ID, but Isadly it's not true... the prefix part of the ID identify the user, not the folder. 
The only way to identify files in specific folder is to use Google Apps Scripting (do a search in StackExchange/StackOverflow and you will find some little useful scripts for doing so), but in my case the command above is more than enough :)

Manuel

rahul286

unread,
Jul 13, 2016, 12:30:48 PM7/13/16
to Google Apps Manager
@Manuel

I came here looking for similar solution.

Do you have any working script to share?

Ideally, I am planning to write 2 scripts which accepts an email address and automate all processes followed during employee onboarding and exit.

Thanks.
Reply all
Reply to author
Forward
0 new messages