Double-sided folder synchronization with robocopy

So, I tried to write a batch file that would sync my local Google Drive folder with my flash drive when I ran it. Since robocopy is a one-way process, I am setting up my code, so it asks you if you want to sync with Google Drive or with Google Drive. This is so that no matter in which of the two places I create something or edit something, I can always synchronize the latest version with both locations.

Before anything happens, the code also checks that it is connected to my computer, and not to anyone else, by checking its name.

Here's what my code looks like right now:

@echo off
If "%computername%"=="JAKE-PC" (
color 02
set /P c=Sync TO or FROM Google Drive[T/F]?

:choice
if /I "%c%" EQU "T" goto :to
if /I "%c%" EQU "F" goto :from
goto :choice

:to
echo Syncing to Google Drive...
robocopy ".\Google Drive\ " "C:\Users\Jake\Google Drive\ " * /mir /xo
goto :done

:from
echo Syncing from Google Drive...
robocopy "C:\Users\Jake\Google Drive\ " ".\Google Drive\ " * /mir /xo
goto :done

:done
echo Sync Complete.               
pause
)

If NOT "%computername%"=="JAKE-PC" (
color 04
echo Not Jake PC!
pause
)

, . -, T, Google . Google , F, Google .

, . "a.txt" - "b.txt" Google , , :

-sync Google , a.txt Google -, b.txt .

-sync FROM Google Drive, b.txt - Google , a.txt .

, , , . ? / . , robocopy.

+4
2

/XX eXclude eXtra files and directories , , :

"" , ; , .

+4

ROBOCOPY /? ( ROBOCOPY.exe doc) /MIR implicite /PURGE:

  /MIR : MIRror a directory tree - equivalent to /PURGE plus all subfolders (/E)
/PURGE : Delete destination files/folders that no longer exist in source.
   /XO : eXclude Older - if destination file exists and is the same date or newer
                         than the source - don’t bother to overwrite it.
    /E : Copy Subfolders, including Empty Subfolders.

/E /MIR :

@echo off

If "%computername%"=="JAKE-PC" goto :sync
color 04
echo Not Jake PC!
goto :done

:sync
color 02

:to
echo Syncing to Google Drive...
robocopy ".\Google Drive\\" "C:\Users\Jake\Google Drive\\" * /E /XO

:from
echo Syncing from Google Drive...
robocopy "C:\Users\Jake\Google Drive\\" ".\Google Drive\\" * /E /XO

echo Sync Complete.               

:done
pause

- ( set /P):

@echo off
SETLOCAL EnableExtensions
If "%computername%"=="JAKE-PC" goto :sync
color 04
echo Not Jake PC!
goto :done

:to
echo Syncing to Google Drive...
robocopy ".\Google Drive\\" "C:\Users\Jake\Google Drive\\" * /E /XO
goto :eof

:from
echo Syncing from Google Drive...
robocopy "C:\Users\Jake\Google Drive\\" ".\Google Drive\\" * /E /XO
goto :eof

:sync
color 02
set /P c=Sync TO or FROM Google Drive or BOTH or NOTHING [T/F/B/N]?
if /I "%c%" EQU "T" ( call :to
  goto :done  
)
if /I "%c%" EQU "F" ( call :from
  goto :done  
)
if /I "%c%" EQU "B" (
  call :to
  call :from
  goto :done  
)
if /I not "%c%" EQU "N" goto :sync
echo No Sync 

:done
echo Sync Complete.               
pause

, GOTO :EOF ( exit /B goto :EOF ). " " , . SETLOCAL EnableExtensions .

GOTO :

GOTO :EOF, script. script .

CALL /? CALL .

, GOTO , FOR IF, .

+3

Source: https://habr.com/ru/post/1610827/


All Articles