Create folder in root, if doesn't exist

190 views
Skip to first unread message

Ben Davidson

unread,
Feb 4, 2022, 5:29:02 PM2/4/22
to GAM for Google Workspace
Hi all,

I'm building a shell script and trying to include some logic to create a folder in the root of a users drive, but only if one of that exact name doesn't exist already.

Creating the folder works fine:

gam user itadmin create drivefile drivefilename "GAM-Logs" mimetype gfolder

But will keep making identically named folders unless I check first. What's the best way to test?

I can get the output of something like

gamma-base-1% gam user itadmin show fileinfo drivefilename "ejfnweofhweofhweoufh"
Getting all Drive Files/Folders that match query ('me' in owners and name = 'ejfnweofhweofhweoufh') for ita...@domain.com
Got 0 Drive Files/Folders that matched query ('me' in owners and name = 'ejfnweofhweofhweoufh') for ita...@domain.com...
User: ita...@domain.com, Show 0 Drive Files/Folders

and grep through it to use in an if/then... for a result like "Got 1 Drive Files/Folders..." or "Got 1 Drive Files/Folders...", but am wondering if there's a more elegant way?

Thanks!

Ross Scroggs

unread,
Feb 4, 2022, 9:16:32 PM2/4/22
to google-ap...@googlegroups.com
Ben,

With Advanced GAM you can do:
$ gam user testsimple show fileinfo query "name = 'SourceFolder' and 'root' in parents" fields id
Getting all Drive Files/Folders that match query (name = 'SourceFolder' and 'root' in parents) for tests...@rdschool.org
Got 1 Drive File/Folder that matched query (name = 'SourceFolder' and 'root' in parents) for tests...@rdschool.org...
User: tests...@rdschool.org, Show 1 Drive File/Folder
  Drive Folder: SourceFolder (16SioWjhoDr9d-IeGofLz-IsGy8fHCSzB)
    id: 16SioWjhoDr9d-IeGofLz-IsGy8fHCSzB
$ echo $?
0


$ gam user testsimple show fileinfo query "name = 'SourceFolderX' and 'root' in parents" fields id
Getting all Drive Files/Folders that match query (name = 'SourceFolderX' and 'root' in parents) for tests...@rdschool.org
Got 0 Drive Files/Folders that matched query (name = 'SourceFolderX' and 'root' in parents) for tests...@rdschool.org...
User: tests...@rdschool.org, Show 0 Drive Files/Folders
admin@academic:~$ echo $?
60

A 0 return code means that the folder exists, 60 means it doesn't

Ross

--
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/94b0cb12-4a92-48dc-ad55-bd192fdb7c0bn%40googlegroups.com.

Ben Davidson

unread,
Feb 7, 2022, 1:33:30 PM2/7/22
to GAM for Google Workspace
Thanks Ross!
Reply all
Reply to author
Forward
0 new messages