---
# System prepended metadata

title: 備份使用者資料夾
tags: [script, windows]

---

# 備份使用者資料夾
###### tags: `windows`, `script`

``` cmd
@echo off
:: 要複製的目錄
set userDirPath=c:\Users
:: 僅複製以下子目錄
set list=Desktop Documents Downloads Favorites

:: 輸出備份的目錄
set backupDirPath=BackupUsers
:: 建立目錄
mkdir %backupDirPath%

:: 獲取目錄
dir /b %userDirPath% > %TEMP%\userID.txt

FOR /F %%i in (%TEMP%\userID.txt) do (

    echo 建立 %%i 資料夾
    mkdir %backupDirPath%\%%i

    for %%l in (%list%) do ( 
        echo = 開始同步 %%i %%l
        robocopy %userDirPath%\%%i\%%l %backupDirPath%\%%i\%%l /MIR /R:1 /W:3 /NDL /NJH /NFL /NJS /MT:2
    )
)


```