【问题标题】:XCOPY: Is it possible that the source folder contains the destination folder?XCOPY:源文件夹是否可能包含目标文件夹?
【发布时间】:2019-06-17 08:23:54
【问题描述】:

我想用 xcopy 复制一个文件夹。但是,只要我将目标文件夹放在源文件夹中,就不会再复制任何内容。这个问题有解决办法吗?

这个想法是将整个文件夹结构(源)的备份生成到一个子文件夹中。执行 xcopy 时,我排除了备份(目标)的子文件夹,我的备份应该存储在其中。

我已经测试了我的代码,只要目标文件夹不在源文件夹中,它就可以正常工作。

代码是用VBA编写的。

Function CopyFolder(ByVal sourcePath As String, ByVal destinationPath As String, ByVal excludeFile As String)
    Dim wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1

    ' /e -> copys all subfolders including empty ones; /k -> retains the read-only attribute if existend
    wsh.Run "xcopy " & sourcePath & " " & destinationPath & " /e /k /exclude:" & excludeFile, vbNormalFocus, waitOnReturn
End Function

代码正在执行,没有错误,但是当我检查目标文件夹时,没有文件被复制。

【问题讨论】:

    标签: vba windows xcopy


    【解决方案1】:

    这应该是不可能的,因为它应该以错误结束:“无法执行循环复制”或类似的东西。

    如果你愿意,你可以做这样的事情(单线):

    FOR /F "usebackq delims=;" %A IN (`dir %sourcePath% /B /AD ^|FINDSTR /V "%destinationPath%"`) DO @xcopy "%sourcePath%\%A\*.*" "%destinationPath%\" /EK
    

    如果 SOURCE 中充满了您应该在 DEST 中复制的目录。包含所有文件的子目录。

    注意:

     ... %A IN (**`** .... **`**)   <---- theese are reverse quotes (alt+96)
    
    ... /AD **^**|FINDSTR /V        <---- this CAP has to be written explicitly like this (but without asterisks, obvious)
    
     %sourcePath% and %destinationPath%     <---  This is the batch variable notation. Do your String concatenation magic here, since you're launching from inside a vba
    

    希望对您有所帮助! :-)

    再见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      相关资源
      最近更新 更多