Get Parent Information till root

348 views
Skip to first unread message

Ashish Jain

unread,
Dec 4, 2023, 7:48:41 AM12/4/23
to GAM for Google Workspace
Is there a way to get parent information of a hierarchical document/folder oneitemperrow?

For eg. here is my document structure

My Drive
    - Parent Folder
        - Child Folder
            - MyDoc

How can I get the folder id's starting from child folder till my drive. Something in below format

owner, id, name, parentid, depth, IsRoot
us...@domain.com,abc123,MyDoc,def456,4,0
us...@domain.com,abc123,Child Folder,ghi789,3,0
us...@domain.com,abc123,Parent Folder,ehd645,2,0
us...@domain.com,abc123,MyDrive,,1,1

Ashish Jain

unread,
Dec 4, 2023, 10:25:38 AM12/4/23
to google-ap...@googlegroups.com
I understand that running the command will fetch all folders and files underneath the specified folder

gam user us...@domain.com print filelist title id mimetype filepath select drivefilename "Parent Folder" showparent

I want the reverse. I will provide the file/folder name and i need the information till root.

---
Ashish


--
You received this message because you are subscribed to a topic in the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-manager/dGACJ1LgKTw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/c0f69390-3e32-4d2e-9a22-6b676519a214n%40googlegroups.com.

Ross Scroggs

unread,
Dec 4, 2023, 10:37:03 AM12/4/23
to google-ap...@googlegroups.com
Ashish,

There is no way to get the file path in this order.

Ross
----
Ross Scroggs



You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/CAHY5XjXZXVBBDJ0ziUDQfe_BXb%3DHQPG9fAPNGqkLpVDsJi8iJQ%40mail.gmail.com.

Ashish Jain

unread,
Dec 4, 2023, 10:38:50 AM12/4/23
to google-ap...@googlegroups.com
Any way to do that recursively? python?

---
Ashish


Michael Porter

unread,
Dec 4, 2023, 11:00:23 AM12/4/23
to google-ap...@googlegroups.com
Hi,

You can do it in Python pretty easily.  drive.get() can be used to get the File Object.  Include "parents" in the field request, then do a get for the parent, and so on.  Eventually, you will get to an object that does not have a parent.  This should either by My Drive, the top of a shared drive, or a file that is orphaned.  Note that "parents" is a list but should only have one object these days.  In the past, a file could have multiple parents, but this should not happen now.

Thanks,

Mike

Mike Porter


Maj Marshall Giguere

unread,
Dec 4, 2023, 12:47:26 PM12/4/23
to google-ap...@googlegroups.com
Ashish;

You can find the parent with gam.  Here's how I get the parent of the leaf node using bash, I'm sure something similar can be done with PowerShell as well:


$> fid='1-0n4nj3-109842-12u3' # leaf fileid
$> fid=$(gam user alp...@example.com print fileinfo $fid fields parents|grep "id:"|(while read tag id;do echo $id;done))

The above will return the id of the parent, you could then feed that back in to find the parent again using a recursive script to walk up the tree until $parentID returns an empty string, the rest is left as an exercise for the student.


Maj Marshall E Giguere

NH Wing Director of IT

Civil Air Patrol, U.S. Air Force Auxiliary

GoCivilAirPatrol.com

nhwg.cap.gov

Volunteers serving America's communities, saving lives, and shaping futures.



Maj Marshall Giguere

unread,
Dec 5, 2023, 12:55:32 PM12/5/23
to google-ap...@googlegroups.com
Here is a demo of walking the parent chain as a bash shell script using nothing by gam

#!/bin/bash -e
# start with a file ID $1 for a given user $2 and walk up the
# parents until reaching the drive top
# file id must be arg $1, user $2
#

fid="$1"
uid="$2"
parentID="$fid"

# recursive function to climb up the parents
function bottomUp () {
    if [ -n "$1" ]; then
echo "$2,$1"
        id=$(gam user $2 print fileinfo "$1" fields parents|grep "id:"|while read tag id;do echo $id;done)
bottomUp "$id" $2
    else
        echo "$2,/root"
    fi
}

# output parents chain from /root to leaf
function topDown () {
    if [ -n "$1" ]; then
        id=$(gam user $2 print fileinfo "$1" fields parents|grep "id:"|while read tag id;do echo $id;done)
topDown "$id" $2
echo "$2,$1"
    else
        echo "$2,/root"
    fi
}

# start at the leaf
echo "Non-recursive"
echo "user,fileid"
echo "$2,$fid"
while [ -n "$parentID" ]; do
    parentID=$(gam user $2 print fileinfo "$parentID" fields parents|grep "id:"|while read tag id;do echo $id;done)
    if [ -z "$parentID" ]; then
        echo "$2,/root"
    else
echo "$2,$parentID"
    fi
done

echo ""
echo "Recursive: bottom up"
echo "user,fileid"
bottomUp "$fid" $2

echo ""
echo "Recursive: top down"
echo "user,fileid"
topDown "$fid" $2

Maj Marshall E Giguere

NH Wing Director of IT

Civil Air Patrol, U.S. Air Force Auxiliary

Volunteers serving America's communities, saving lives, and shaping futures.

Ross Scroggs

unread,
Dec 6, 2023, 10:36:19 AM12/6/23
to google-ap...@googlegroups.com
I'm working on this.

Ross
----
Ross Scroggs


On Dec 4, 2023, at 7:25 AM, Ashish Jain <jain.a...@gmail.com> wrote:

You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/CAHY5XjXZXVBBDJ0ziUDQfe_BXb%3DHQPG9fAPNGqkLpVDsJi8iJQ%40mail.gmail.com.

Ross Scroggs

unread,
Dec 6, 2023, 1:47:49 PM12/6/23
to google-ap...@googlegroups.com
Ross
----
Ross Scroggs


--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.

Ashish Jain

unread,
Dec 7, 2023, 12:31:20 AM12/7/23
to google-ap...@googlegroups.com
This works great. Is there a way to add the original fileid in the returned columns so that I can filter it in csv?

---
Ashish


You received this message because you are subscribed to a topic in the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-manager/dGACJ1LgKTw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/7CCF40A9-082B-46B0-8E4F-56E6E5AF8AC5%40gmail.com.

Julio Moran

unread,
Dec 7, 2023, 6:48:53 AM12/7/23
to google-ap...@googlegroups.com

2-Step Verification required for admins starting December 19

Starting December 19, all admins are required to turn on 2-Step Verification. Check your admins' status and ensure they take action to avoid account lockout.
We have Service accounts that will be affected with this, do you have any options to set up these accounts in GAMADV-XTD3?


Ross Scroggs

unread,
Dec 7, 2023, 10:12:51 AM12/7/23
to google-ap...@googlegroups.com
I can, are there any other changes?

Ross
----
Ross Scroggs


Ashish Jain

unread,
Dec 7, 2023, 10:25:03 AM12/7/23
to GAM for Google Workspace
No other changes. Thanks a lot Ross. Appreciate the consideration.

Ross Scroggs

unread,
Dec 7, 2023, 11:37:07 AM12/7/23
to google-ap...@googlegroups.com
Update to 6.66.08

Ross
----
Ross Scroggs


Ashish Jain

unread,
Dec 8, 2023, 6:46:45 AM12/8/23
to google-ap...@googlegroups.com
Works perfectly. Thanks a lot, Ross.

---
Ashish


Reply all
Reply to author
Forward
0 new messages