Robocopy - 从子目录中的清除中排除特定文件

Dae*_*eze 6 robocopy

我想在仍然执行清除的同时排除特定文件被删除。特定文件位于源中不存在的子目录中。

来源:

folder1\
    file1.txt
Run Code Online (Sandbox Code Playgroud)

目的地:

folder1\
    file1.txt
    folder2\
        dontdelete.txt
        delete.txt
Run Code Online (Sandbox Code Playgroud)

如果我使用:

Robocopy C:\Source C:\Destination /e /purge /xf dontdelete.txt
Run Code Online (Sandbox Code Playgroud)

然后 Robocopy 将删除包含该文件的 folder2\,因此本质上仍然删除 dontdelete.txt 文件。

Source : C:\Source\
Dest : C:\Destination\
Files : *.*     
Exc Files : dontdelete.txt      
Options : *.* /V /L /S /E /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30 
----------------------------------------------------------------------------
                   0    C:\Source\
                   1    C:\Source\folder1\
*EXTRA Dir        -1    C:\Destination\folder1\folder2\
  *EXTRA File              0    delete.txt
  *EXTRA File              0    dontdelete.txt
          same             0    file1.txt
Run Code Online (Sandbox Code Playgroud)

如果我使用:

Robocopy C:\Source C:\Destination /e /purge /xd folder2 /xf dontdelete.txt
Run Code Online (Sandbox Code Playgroud)

然后 Robocopy 根本不会在 folder2 内部查找应该清除的文件。

Source : C:\Source\
Dest : C:\Destination\
Files : *.*     
Exc Files : dontdelete.txt      
Exc Dirs : folder2      
Options : *.* /V /L /S /E /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30 
----------------------------------------------------------------------------
                   0    C:\Source\
                   1    C:\Source\folder1\
  *named dir      -1    C:\Destination\folder1\folder2\
          same             0    file1.txt
Run Code Online (Sandbox Code Playgroud)

我还尝试使用包括文件在内的整个路径,而输出没有差异。

小智 1

提及完整目标文件的路径以将其从清除中排除。所以,而不是

Robocopy C:\Source C:\Destination /e /purge /xf dontdelete.txt

使用:

Robocopy C:\Source C:\Destination /e /purge /xf C:\Destination\folder1\folder2\dontdelete.txt

这适用于/purge/mir。所以可以在命令中/e /purge替换为,因为它们本质上是相同的。/mir所以命令可以是:

Robocopy C:\Source C:\Destination /mir /xf C:\Destination\folder1\folder2\dontdelete.txt

基于这个答案