Batch script đồng bộ folder trên windows

Hiện tại, Windows chưa phát triển một công cụ nào thực hiện việc đồng bộ 2 thư mục trên cùng 1 server.

Trong khi đó, việc sử dụng các công cụ download trên internet mang lại nhiều rủi ro về bảo mật.

Dưới đây là hướng dẫn sử dụng script do đội ngũ CloudCraft phát triển để thực hiện việc đồng bộ dữ liệu giữa 2 thư mục:

_ Truy cập đường dẫn sau để download và giải nén file script:

https://github.com/cloudcraftteam/Sync-folders

_ Mở cửa sổ command line và di chuyển tới thư mục chứa file script.

(hoặc mở thư mục chứ file script, bấm Shift + chuột phải => chọn Open command window here)

_ Thực thi script với cú pháp sau:
sync.bat <source> <dest> <timeout>

Trong đó:

timeout: thời gian chờ giữa 2 lần sync, tính bằng giây (để trống sẽ thực hiện đồng bộ 1 lần duy nhất)

Ví dụ:
sync.bat C:\homework D:\secret 5

Lệnh trên sẽ thực hiện sync từ thư mục C:\homework tới thư mục D:\secret, thời gian giữa các lần sync là 5 giây

Lưu ý:

  • Cần phải đảm bảo thư mục nguồn và thư mục đích đã được tạo.
  • Phải giữ cửa sổ command line luôn mở (nếu đóng thì chương trình sẽ dừng).
  • Đường dẫn thư mục nguồn và thư mục đích không được có dấu hoặc kí tự đặt biệt, ví dụ: #, $, @, %…

Nội dung file sync.bat

@echo off

set “source=%1”
set “dest=%2”
set “timeout=%3”

::::::::::::::REPLACE INCORRECT SEPERATOR FROM / TO \::::::::::::::::
set replace=\
call set source=%%source:/=%replace%%%
call set dest=%%dest:/=%replace%%%

::::::::::::::::::::REMOVE ENDING SEPERATOR::::::::::::::::::::::::::
:while1
if “%source:~-1%”==”\” (
    set “source=%source:~0,-1%”
    goto while1
)

:while2
if “%dest:~-1%”==”\” (
    set “dest=%dest:~0,-1%”
    goto while2
)

::::::::::::::::CHECK IF INPUT TIMEOUT VALID OF NOT::::::::::::::::::
if “%timeout%” == “” (
    set “timeout=-1”
    echo [%date% %time%] SYNC START >> sync-log.txt
    goto loop
)
echo %timeout%| findstr /r “^[1-9][0-9]*$” > nul
if %errorlevel% neq 0 (
    echo INVALID TIMEOUT…
    echo “Usage: .\sync.bat <source> <dest> <timeout>”
    pause
    exit
) else (
    echo [%date% %time%] SYNC START >> sync-log.txt
)

::::::::::::::::::::::::::BEGIN SYNC:::::::::::::::::::::::::::::::::
:loop

xcopy /r /e /d /c /y %source% %dest% | find /v “File(s) copied” >> sync-log.txt

dir /b /s %dest%  > dest.txt

set “find=%dest%\”
set “replace=”
set “textfile=dest.txt”
set “newfile=output.txt”
(for /f “delims=” %%i in (%textfile%) do (
    set “line=%%i”
    setlocal enabledelayedexpansion
    set “line=!line:%find%=%replace%!”
    echo(!line!
    endlocal
    )
) > %newfile%
del %textfile%
rename %newfile%  %textfile%

for /f “tokens=*” %%F in (dest.txt) do (
    if not exist “%source%\%%F” (
        if exist “%dest%\%%F” (
            echo delete %dest%\%%F >> sync-log.txt
            if exist “%dest%\%%F\*” (
                rmdir /s /q “%dest%\%%F”
            ) else (
                del /q “%dest%\%%F”
            )
            if exist “%dest%\%%F” echo delete %dest%\%%F fail >> sync-log.txt
        )
    )
)

if exist “dest.txt” del /q dest.txt

if “%timeout%” neq “-1” (
    timeout %timeout% > nul
    goto loop
) else (
    echo COMPLETE…
    echo COMPLETE… >> sync-log.txt
)

Chúc bạn thành công.

Tin cùng chuyên mục

Hướng Dẫn Sửa Lỗi Không Cài Được Email 365 Trên Outlook 2016

Hướng Dẫn Sửa Lỗi Không Cài Được Email 365 Trên Outlook 2016...

Hướng Dẫn Limit Session Login RDP Trên Windows

Giới hạn số lượng phiên đăng nhập session qua Remote Desktop...

Cài đặt mail outlook trên điện thoại Android

Hướng dẫn sử dụng Mở app Outlook Chọn thêm tài khoản Nhập tà...

Hướng dẫn fix lỗi Windows Cannot Find

Hướng dẫn fix lỗi Windows Cannot Find “C:\WINDOWS\system32\T...