Get path of the Google Drive folder

Share your advanced PureBasic knowledge/code with the community.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Get path of the Google Drive folder

Post by Little John »

Hi,

for some programs it is useful to know the location of the Google Drive folder on a given system.
(see also my tip "Get path of the Dropbox folder")

Unfortunately I have no opportunity to test this code under Mac OS. So I would highly appreciate it if someone else would do so. :-)

Code: Select all

; Code successfully tested with Google Backup and Sync 3.47
; and
; [v] PB 5.71 LTS (x64)
; on
; [v] Windows 10 Pro, version 1903
; [ ] Mac OS


CompilerIf #PB_Compiler_IsMainFile
   EnableExplicit
CompilerEndIf


Procedure.s GoogleDriveFolder ()
   ; -- return the main folder used by "Google Backup and Sync" client,
   ;    with trailing path separator, or "" if not found
   ; after <https://stackoverflow.com/questions/12173077/how-do-i-programmatically-locate-my-google-drive-folder-using-c>
   Protected.i db, i
   Protected dbFile$, query$, ret$=""
   
   CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Windows
         dbFile$ = GetEnvironmentVariable("localappdata") + "\Google\Drive\user_default\sync_config.db"
         
      CompilerCase #PB_OS_MacOS
         ; after <https://stackoverflow.com/questions/30609133/how-do-i-programmatically-locate-my-google-drive-folder-in-osx>
         ; (not yet tested)
         dbFile$ = "~/Library/Application Support/Google/Drive/user_default/sync_config.db"
         
      CompilerCase #PB_OS_Linux
         CompilerError "Sorry, GoogleDriveFolder() is not supported on Linux"
         
         ; In contrast to Windows and OSX, for Linux there is no official
         ; "Google Backup and Sync" client. One proposed solution is to
         ; integrate Google Drive using Gnome Online Accounts, see e.g.
         ; <https://www.itzgeek.com/how-tos/linux/linux-mint-how-tos/how-to-integrate-google-drive-on-linux-mint-19-tara.html>.
         ; However, this does not work on my Linux Mint 19.2 (x64) system.
         ;
         ; There are several third party Linux programs for synchronisation
         ; with Google Drive. The name and location of the corresponding
         ; folder depends on the particular program, so it's not possible
         ; to provide a general solution here.
   CompilerEndSelect
   
   db = OpenDatabase(#PB_Any, dbFile$, "", "", #PB_Database_SQLite)
   If db
      query$ = "SELECT * FROM data WHERE entry_key='local_sync_root_path'"
      If DatabaseQuery(db, query$)
         If NextDatabaseRow(db)
            i = DatabaseColumnIndex(db, "data_value")
            ret$ = GetDatabaseString(db, i)
            If FileSize(ret$) <> -2          ; If 'ret$' is not an existing folder
               ret$ = ""
            EndIf   
         EndIf
         FinishDatabaseQuery(db)
      EndIf
      CloseDatabase(db)
   EndIf
   
   If Asc(ret$) <> '' And Right(ret$, 1) <> #PS$
      ret$ + #PS$
   EndIf
   ProcedureReturn Mid(ret$, 5)   ; discard leading "\\?\"
EndProcedure


CompilerIf #PB_Compiler_IsMainFile
   ; -- Demo
   
   UseSQLiteDatabase()
   
   Define GoogleDrive$ = GoogleDriveFolder()
   
   Debug "Location of Google Drive folder:"
   If Asc(GoogleDrive$) <> ''
      Debug "'" + GoogleDrive$ + "'"
   Else
      Debug "not found"
   EndIf
CompilerEndIf