双向文件夹与robocopy同步

时间:2015-10-08 17:42:53

标签: batch-file

所以我一直在尝试编写一个批处理文件,当我运行它时,它会将我的本地Google云端硬盘文件夹与我的闪存驱动器同步。由于robocopy是一个单向进程,因此我设置了代码,因此它会询问您是要同步到Google云端硬盘还是从Google云端硬盘进行同步。这样,无论我在哪个位置制作或编辑某些内容,我都可以将最新版本同步到这两个位置。

在发生任何事情之前,代码还会通过检查其名称来验证它是否已插入我的计算机而不是其他人。

这就是我现在的代码:

@echo off
If "%computername%"=="JAKE-PC" (
color 02
set /P c=Sync TO or FROM Google Drive[T/F]?

:choice
if /I "%c%" EQU "T" goto :to
if /I "%c%" EQU "F" goto :from
goto :choice

:to
echo Syncing to Google Drive...
robocopy ".\Google Drive\ " "C:\Users\Jake\Google Drive\ " * /mir /xo
goto :done

:from
echo Syncing from Google Drive...
robocopy "C:\Users\Jake\Google Drive\ " ".\Google Drive\ " * /mir /xo
goto :done

:done
echo Sync Complete.               
pause
)

If NOT "%computername%"=="JAKE-PC" (
color 04
echo Not Jake's PC!
pause
)

似乎工作正常。如果我对闪存驱动器进行了更改,我会运行批处理文件并输入T,这会将我的更改同步到Google云端硬盘。如果我在Google云端硬盘上进行了更改,请输入F,它将同步Google云端硬盘中的更改。

然而,只有一个缺陷,那就是每个位置都有要同步的新内容。如果我在我的闪存驱动器上创建“a.txt”并在Google云端硬盘上创建“b.txt”,那么如果我:

-sync对于Google云端硬盘,a.txt将从我的闪存驱动器复制到Google云端硬盘,但b.txt已从两者中删除。

-sync FROM Google Drive,b.txt将从Google云端硬盘复制到我的闪存驱动器,但a.txt已从两者中删除。

因此,如果两个位置都有新内容,这会导致文件永远丢失的问题。有没有什么办法解决这一问题?我只需要一种同步两个位置的双向方法,而不会丢失其中一个位置的文件/文件夹。甚至可能没有robocopy。

2 个答案:

答案 0 :(得分:4)

/XX切换到eXclude eXtra files and directories应根据this answer执行您需要的操作:

  

“额外”文件出现在目的地但不是来源;排除额外内容将阻止目的地的任何删除。

答案 1 :(得分:4)

根据ROBOCOPY /?(或ROBOCOPY.exe doc),/MIR选项强制隐含/PURGE

  /MIR : MIRror a directory tree - equivalent to /PURGE plus all subfolders (/E)
/PURGE : Delete destination files/folders that no longer exist in source.
   /XO : eXclude Older - if destination file exists and is the same date or newer
                         than the source - don’t bother to overwrite it.
    /E : Copy Subfolders, including Empty Subfolders.

使用/E代替/MIR选项,并以无人值守的方式执行复制:

@echo off

If "%computername%"=="JAKE-PC" goto :sync
color 04
echo Not Jake's PC!
goto :done

:sync
color 02

:to
echo Syncing to Google Drive...
robocopy ".\Google Drive\\" "C:\Users\Jake\Google Drive\\" * /E /XO

:from
echo Syncing from Google Drive...
robocopy "C:\Users\Jake\Google Drive\\" ".\Google Drive\\" * /E /XO

echo Sync Complete.               

:done
pause

或类似下一代码段的内容(保持set /P用户的插入):

@echo off
SETLOCAL EnableExtensions
If "%computername%"=="JAKE-PC" goto :sync
color 04
echo Not Jake's PC!
goto :done

:to
echo Syncing to Google Drive...
robocopy ".\Google Drive\\" "C:\Users\Jake\Google Drive\\" * /E /XO
goto :eof

:from
echo Syncing from Google Drive...
robocopy "C:\Users\Jake\Google Drive\\" ".\Google Drive\\" * /E /XO
goto :eof

:sync
color 02
set /P c=Sync TO or FROM Google Drive or BOTH or NOTHING [T/F/B/N]?
if /I "%c%" EQU "T" ( call :to
  goto :done  
)
if /I "%c%" EQU "F" ( call :from
  goto :done  
)
if /I "%c%" EQU "B" (
  call :to
  call :from
  goto :done  
)
if /I not "%c%" EQU "N" goto :sync
echo No Sync 

:done
echo Sync Complete.               
pause

请注意,如果Command Extensions被停用GOTO will no longer recognise the :EOF label(在案例中使用exit /B而不是goto :EOF)。虽然Command Extensions are enabled by default,但我们不能在其中推测。这就是我使用SETLOCAL EnableExtensions作为一般原则的原因。

  

如果启用了命令扩展,则GOTO更改如下:

     

GOTO命令现在接受传输的:EOF目标标签   控制到当前批处理脚本文件的末尾。这很容易   在不定义标签的情况下退出批处理脚本文件的方法。

     

键入CALL /?以获取CALL命令扩展名的说明   使这个功能有用。

另请注意Using GOTO within parentheses - including FOR and IF commands - will break their context

相关问题