首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批处理文件以迭代外部硬盘驱动器的所有组合

批处理文件以迭代外部硬盘驱动器的所有组合
EN

Stack Overflow用户
提问于 2017-08-11 11:19:03
回答 1查看 200关注 0票数 0

我有许多外部硬盘驱动器,其中包含我的库文件夹的备份,并希望使用Robocopy保持所有的同步。因此,我的问题是如何在批处理文件中迭代所有外部驱动器的组合?

例如,包含以下库文件夹的三个外部硬盘驱动器:E:\库、G:\库和N:\库

  1. 如果E:\库,那么机器人将G:\库复制到N:\库
  2. 如果G:\库,那么机器人将E:\库复制到N:\库
  3. 如果N:\库,那么机器人将E:\库复制到G:\库

等等,用于连接的外部硬盘驱动器的所有可能组合。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-11 14:07:09

一个简单的选择是首先沿着磁盘向一个方向(从低到高的驱动器)传播更改,然后在oposite方向传播更改。

代码语言:javascript
复制
        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

可作为

代码语言:javascript
复制
@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以执行复制操作

编辑了以响应注释-为了排除目标中不存在的一些文件夹,您可以尝试使用以下内容

代码语言:javascript
复制
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! 
) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45633947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档