我有许多外部硬盘驱动器,其中包含我的库文件夹的备份,并希望使用Robocopy保持所有的同步。因此,我的问题是如何在批处理文件中迭代所有外部驱动器的组合?
例如,包含以下库文件夹的三个外部硬盘驱动器:E:\库、G:\库和N:\库
等等,用于连接的外部硬盘驱动器的所有可能组合。
发布于 2017-08-11 14:07:09
一个简单的选择是首先沿着磁盘向一个方向(从低到高的驱动器)传播更改,然后在oposite方向传播更改。
Drives A: B: C: D: E:
-------------------------------------------------------------
Data inside A B C D E
-------------------------------------------------------------
copy operations
> 1 A > AB C D E
> 2 A AB > ABC D E
> 3 A AB ABC > ABCD E
> 4 A AB ABC ABCD > ABCDE
-------------------------------------------------------------
< 5 A AB ABC ABCDE < ABCDE
< 6 A AB ABCDE < ABCDE ABCDE
< 7 A ABCDE < ABCDE ABCDE ABCDE
< 8 ABCDE < ABCDE ABCDE ABCDE ABCDE可作为
@echo off
setlocal enableextensions enabledelayedexpansion
set "librariesFolder=\Libraries"
set nDrives=0
rem Enumerate available disks
for /f "tokens=2 delims=:=" %%a in ('
wmic logicaldisk get name /value
') do (
rem Check if drive can be accessed and contains libraries folder
>nul 2>nul vol %%a: && if exist "%%a:%librariesFolder%" (
set /a nDrives+=1
set "drive!nDrives!=%%a"
)
)
if %nDrives% lss 2 (
echo Not enough drives found to sync
goto :eof
)
rem Propagate changes from fist disk to last
set "s=" & for /l %%t in (1 1 %nDrives%) do (
if defined s for %%s in (!s!) do (
echo robocopy "!drive%%s!:%librariesFolder%\." "!drive%%t!:%librariesFolder%\." /s
)
set "s=%%t"
)
rem Propagate changes from last disk to first
set "s=" & for /l %%t in (%nDrives% -1 1) do (
if defined s for %%s in (!s!) do (
echo robocopy "!drive%%s!:%librariesFolder%\." "!drive%%t!:%librariesFolder%\." /s
)
set "s=%%t"
)注意,robocopy操作以echo命令作为前缀,以避免任何更改。如果控制台的输出是正确的,那么删除echo以执行复制操作
编辑了以响应注释-为了排除目标中不存在的一些文件夹,您可以尝试使用以下内容
if defined s for %%s in (!s!) do (
set "excludeFolders="
for %%x in ("Camera Movies" "Documents" "Downloads") do (
if not exist "!drive%%t!:%librariesFolder%\%%~x\" (
set excludeFolders=!excludeFolders! %%x
)
)
if defined excludeFolders set excludeFolders=/XD !excludeFolders!
robocopy.exe "!drive%%s!:%librariesFolder%\." "!drive%%t!:%librariesFolder%\." /S !excludeFolders!
) https://stackoverflow.com/questions/45633947
复制相似问题