Files sync Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Files sync Help

#1 Post by born2achieve » 19 Mar 2019 19:04

Hello Friends,

i need to sync a file b/w two folders in network path bi directionally. for example, if new file /modified file on server A automatically transferred to ServerB and vise versa. i tried to look the solution online and robo copy an be used for this. i tried. but i am not getting what i am looking for. here is my try.

Code: Select all

robocopy \\server1\Images1 \\server2\Images1 /MIR /FFT /Z /XA:H /W:5
can you please help in by giving batch script to achieve this. Thanks in advance.

aGerman
Expert
Posts: 3852
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Files sync Help

#2 Post by aGerman » 20 Mar 2019 06:20

As far as I know Windows doesn't have any tool on board to synchronize folders bi directionally.

Steffen

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Files sync Help

#3 Post by born2achieve » 20 Mar 2019 06:34

Thanks for the reply and in that case can i run the batch script on both the server.

Server A:

If new file or any file changed, take the file and look in server B. if not found add it else replace it.

Server B

If new file or any file changed, take the file and look in server A. if not found add it else replace it.

Will this logic work? if yes then could you please share me the script for one server.

aGerman
Expert
Posts: 3852
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Files sync Help

#4 Post by aGerman » 20 Mar 2019 07:11

By default robocopy will only copy new or changed files. So what you could try is running two robocopy commands (one for each direction) in a loop. But you definitely have to omit the /mir switch because otherwise you would loose files that still don't exist in one of the folders. That also means that it will not be a synchronization anymore because files that you actively delete will not be automatically deleted in the other folder.

Steffen

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Files sync Help

#5 Post by born2achieve » 20 Mar 2019 07:46

Thank you for the reply and would you be able to modify my script and provide the clean script for my requirement.

aGerman
Expert
Posts: 3852
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Files sync Help

#6 Post by aGerman » 20 Mar 2019 09:10

Something like so

Code: Select all

@echo off
for /l %%i in () do (
  robocopy "\\server1\Images1" "\\server2\Images1"
  robocopy "\\server2\Images1" "\\server1\Images1"
  timeout /t 10 /nobreak
)
Just read the help message of robocopy if you think you'd need any additional options.

Steffen

HannahDaniel
Posts: 6
Joined: 20 Feb 2020 06:40

Re: Files sync Help

#7 Post by HannahDaniel » 22 Feb 2020 05:21

check the below script that allows you to copy modified and new files and folders

Code: Select all

@echo off
:: It is a total copy first and then incrementally,
:: ie, it just copies the new files and changed files.
mode con cols=95 lines=5 & color 0E
Title %~nx0 for Incremental Backup with XCopy Command by HD 2020
set "Settings=%~dp0Settings.ini"
Set "FirstFull_CopyLog=%~dp0%~n0_FirstFull_CopyLog.txt"
Set "LogFile=%~dp0%~n0_Incremental_CopyLog.txt"
If not exist "%Settings%" (
	Call :BrowseForFolder "Please choose the source folder for the backup" SourceFolder
	Setlocal EnableDelayedExpansion
	If defined SourceFolder (
		echo(
		echo             You chose "!SourceFolder!" as source folder
	) else (
		echo(
		Color 0C & echo                    The source folder is not defined ... Exiting ......
		Timeout /T 2 /nobreak>nul & exit
	)
	Call :BrowseForFolder "Please choose the target folder for the backup" TargetFolder
	If defined TargetFolder (
		echo(
		echo             You chose "!TargetFolder!" as Target folder
	) else (
		echo(
		Color 0C & echo                    The Target folder is not defined ... Exiting ......
		Timeout /T 2 /nobreak>nul & exit
	)
Timeout /T 3 /nobreak>nul
	(
		echo "!SourceFolder!" 
		echo "!TargetFolder!\Backups_%ComputerName%\" 
	)
cls & echo( & echo(
echo         Please wait a while ... The Backup to "!TargetFolder!" is in progress... 
Call :Backup_XCopy "!SourceFolder!" "!TargetFolder!" "!FirstFull_CopyLog!"
Timeout /T 1 /nobreak>nul 
Start "" "!FirstFull_CopyLog!" & exit
) else (
Setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%Settings%"') do (
	set /a idx+=1
	set Param[!idx!]=%%a
)

Set "SourceFolder=!Param[1]!"
Set "TargetFolder=!Param[2]!"
echo(
echo        The source Folder from Settings.ini is : !SourceFolder!
echo        The Target Folder from Settings.ini is : !TargetFolder!
Timeout /T 1 /nobreak>nul & cls & echo( & echo(
echo       Please wait a while ... The Backup to !TargetFolder! is in progress... 
Call :Backup_XCopy !SourceFolder! !TargetFolder! !LogFile!
)
Timeout /T 1 /nobreak>nul 
Start "" !LogFile! & exit
::****************************************************************************
:BrowseForFolder
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'%1',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "%2=%%I"
exit /b
::****************************************************************************
:Backup_XCopy <Source> <Target> <LogFile>
Xcopy /c  /d  /e  /s  /i  /y %1 %2 > %3 2>&1
Exit /b
::****************************************************************************
also if you need to do it easier just switch to robocopy alternatives which will do the same process without consuming time like gs richcopy360 and securecopy quest .

Digi321
Posts: 1
Joined: 03 Mar 2020 00:15

Re: Files sync Help

#8 Post by Digi321 » 03 Mar 2020 00:32

its way to sync your files and data
it just copies the new files and changed files.

Code: Select all

mode con cols=95 lines=5 & color 0E
Title %~nx0 for Incremental Backup with XCopy Command by HD 2020
set "Settings=%~dp0Settings.ini"
Set "FirstFull_CopyLog=%~dp0%~n0_FirstFull_CopyLog.txt"
Set "LogFile=%~dp0%~n0_Incremental_CopyLog.txt"
If not exist "%Settings%" (
	Call :BrowseForFolder "Please choose the source folder for the backup" SourceFolder
	Setlocal EnableDelayedExpansion
	If defined SourceFolder (
		echo(
		echo             You chose "!SourceFolder!" as source folder
	) else (
		echo(
		Color 0C & echo                    The source folder is not defined ... Exiting ......
		Timeout /T 2 /nobreak>nul & exit
	)
	Call :BrowseForFolder "Please choose the target folder for the backup" TargetFolder
	If defined TargetFolder (
		echo(
		echo             You chose "!TargetFolder!" as Target folder
	) else (
		echo(
		Color 0C & echo                    The Target folder is not defined ... Exiting ......
		Timeout /T 2 /nobreak>nul & exit
	)
Timeout /T 3 /nobreak>nul
	(
		echo "!SourceFolder!" 
		echo "!TargetFolder!\Backups_%ComputerName%\" 
	)
cls & echo( & echo(
echo         Please wait a while ... The Backup to "!TargetFolder!" is in progress... 
Call :Backup_XCopy "!SourceFolder!" "!TargetFolder!" "!FirstFull_CopyLog!"
Timeout /T 1 /nobreak>nul 
Start "" "!FirstFull_CopyLog!" & exit
) else (
Setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%Settings%"') do (
	set /a idx+=1
	set Param[!idx!]=%%a
)

Set "SourceFolder=!Param[1]!"
Set "TargetFolder=!Param[2]!"
echo(
echo        The source Folder from Settings.ini is : !SourceFolder!
echo        The Target Folder from Settings.ini is : !TargetFolder!
Timeout /T 1 /nobreak>nul[url=https://www.ambiguousit.com/services/digital-marketing/]cls &[/url] cls & echo( & echo(
echo       Please wait a while ... The Backup to !TargetFolder! is in progress... 
Call :Backup_XCopy !SourceFolder! !TargetFolder! !LogFile!
)
Timeout /T 1 /nobreak>nul 
Start "" !LogFile! & exit
::****************************************************************************
:BrowseForFolder
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'%1',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "%2=%%I"
exit /b
::****************************************************************************
:Backup_XCopy <Source> <Target> <LogFile>
Xcopy /c  /d  /e  /s  /i  /y %1 %2 > %3 2>&1
Exit /b
Last edited by Squashman on 03 Mar 2020 08:18, edited 1 time in total.
Reason: MOD EDIT: Please use code tags.

Post Reply