Backup GoogleDrive files to local PC automatically

Post your working scripts, libraries and tools.
hvegte
Posts: 1
Joined: 01 Feb 2021, 07:51

Backup GoogleDrive files to local PC automatically

Post by hvegte » 04 Jul 2022, 10:59

Code: Select all

;
; AutoHotkey Version: 2.0
; Language:       English
; Platform:       Win9x/NT
; Author:         H. de Vegte Copyright 2022
;
; Script Function:
;	This script makes smart copies of files from your Google Drive to the C or D drive of your computer.
;       The destination directory on your C or D drive is ~SmartBackupMyGoogleDrive and will be created if it does not yet exist.
;       When the file to be copied already exists in the destination directory and is exactly the same, it will not be copied.
;       However, if there is any change in the document since the last copy, it will be copied. The filename in the destination
;       directory will be extended with a timestamp, so that several versions of the same file can exist in the destination 
;       directory. This way the program creates a document history. This enables you to retrieve a previous version of e.g. 
;       a document.
;       The destination directory contains the same sub-directory structure as the GoogleDrive source directory.
;       Within the destination directory, logfile ~MyGoogleDriveFilesFound.txt is maintained, so that you always can check 
;       which files were copied and on which date.
;       The first time of execution the script runs a while in the background, depending on the size of your Google Drive.
;       After the first run, the script runs very fast.
;
; NOTE: If the root directory on your Google Drive is NOT 'Mijn Drive' but something like 'My Drive' or any other name, 
;       you need to modify 10 lines in this script to mention your own Google Drive root directory. Explanation is given furtheron in
;       this script.
;
; RECOMMENDATION: Include this script in your Windows auto-startup directory.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance Ignore

;;;^#Y::

; Beeldschermresolutie: 1280x720
Process, Priority, , Low
SetBatchLines -1

Sleep, 30000 ; Because this script is often used as one of the auto-startup programs, a pause of 30 seconds is done to enable the internet connection to startup, before we can start our process.

pos1=0
pos2=0
filenamefound=
mapfound=
ReadFrom=C
WriteTo=D
; Check if a C drive exists 
; If a D drive exists, the output goes to the D drive. Else the output goes to the C drive.

DriveSpaceFree, FreeSpace, %ReadFrom%:\
If  FreeSpace<0
    {
     MsgBox, No C drive found, so the program will not be executed.
     ExitApp
    } 

DriveSpaceFree, FreeSpace, %WriteTo%:\
If  FreeSpace<0
    {
     WriteTo=C
    } 

FileCreateDir, %WriteTo%:\~SmartBackupMyGoogleDrive 

ErrorLevel=0
;;;;;var += 0, S  ;This retrieves the date and time in seconds


FileCreateDir, %WriteTo%:\~SmartBackupMyGoogleDrive            
ErrorLevel=0

IfNotExist, %WriteTo%:\~SmartBackupMyGoogleDrive\~MyGoogleDriveFilesFound.txt
    {
        FileAppend , , %WriteTo%:\~SmartBackupMyGoogleDrive\~MyGoogleDriveFilesFound.txt       
        Sleep, 100 
    }  

DriveSpaceFree, FreeSpace, %ReadFrom%:\
If  FreeSpace<0
    {
     MsgBox, No C drive is found, therefore the program can not continue.
     ExitApp
    } 

DriveSpaceFree, FreeSpace, %WriteTo%:\
If  FreeSpace<0
    {
     WriteTo=C
    } 

; Now we try to find the drive character where our Google Drive should be.

; NOTE!!! Because of language differences, it might be that you need to modify the 'Mijn Drive' text's below to the name of your main directory which could be e.g. 'My Drive' or something simular.
;         This should be done for 10 occurrences, the 9 below, but also the one in the loop. 


if FileExist("E:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=E
if FileExist("F:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=F
if FileExist("G:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=G
if FileExist("H:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=H
if FileExist("I:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=I
if FileExist("J:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=J
if FileExist("K:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=K
if FileExist("L:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=L
if FileExist("M:\Mijn Drive")      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    ReadFrom=M

teller=0
    Loop Files, %ReadFrom%:\Mijn Drive\*.*, R  ; Recurse into subfolders.      ; <------  Modify 'Mijn Drive' to your own 'My Drive' or something simular, which is the name of the main directory in your Google drive
    {    
         if A_LoopFileExt=ini
            continue
         if A_LoopFileExt=gdoc
            continue
         mapfound := SubStr(A_LoopFileDir, 15) 
         FileCreateDir, %WriteTo%:\~SmartBackupMyGoogleDrive\%mapfound%            
         ErrorLevel=0
         StringGetPos, pos1, A_LoopFileFullPath, \, R1                  
         StringGetPos, pos2, A_LoopFileFullPath, ., R1                  
         filenamefound := SubStr(A_LoopFileFullPath, pos1+2, pos2-pos1-1)        ;>>> filenamefound is filename without the extention

         IfNotExist, %WriteTo%:\~SmartBackupMyGoogleDrive\%mapfound%\%filenamefound%~%A_LoopFileTimeModified%.%A_LoopFileExt%
              {
               teller +=1
               FileCopy, %A_LoopFileFullPath%, %WriteTo%:\~SmartBackupMyGoogleDrive\%mapfound%\%filenamefound%~%A_LoopFileTimeModified%.%A_LoopFileExt%      
               FileAppend, %teller% Backup made: %A_DD%-%A_MMM%-%A_YYYY% %A_Hour%:%A_Min%  %A_LoopFileFullPath% `n, %WriteTo%:\~SmartBackupMyGoogleDrive\~MyGoogleDriveFilesFound.txt  
              }
    }

SoundBeep, 750, 200
SoundBeep, 750, 200
SoundBeep, 750, 200

if teller=0
        {
         FileAppend, SmartBackup completed: %A_DD%-%A_MMM%-%A_YYYY% %A_Hour%:%A_Min%  No new or modified files are found`n, %WriteTo%:\~SmartBackupMyGoogleDrive\~MyGoogleDriveFilesFound.txt  
         MsgBox, , SmartBackup completed, No new or modified files are added to the %WriteTo%:\~SmartBackupMyGoogleDrive directory
        }
else
        if teller=1
              MsgBox, , SmartBackup completed, There is 1 new or modified file added to the %WriteTo%:\~SmartBackupMyGoogleDrive directory
        else
              MsgBox, , SmartBackup completed, There are %teller% new or modified files added to the %WriteTo%:\~SmartBackupMyGoogleDrive directory

ExitApp ; This is the end of the program.
Last edited by BoBo on 04 Jul 2022, 11:15, edited 1 time in total.
Reason: Added [code][/code]-tags.

Return to “Scripts and Functions (v2)”