我想用xcopy复制一个文件夹。但是,一旦将目标文件夹放在源文件夹中,就不再复制任何内容。是否有解决此问题的方法?
这个想法是将整个文件夹strucutre(源)备份到一个子文件夹中。执行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
代码正在正确执行,但是当我检查目标文件夹时,没有文件被复制。
答案 0 :(得分:0)
这不可能,因为它应该以错误:“无法执行循环复制”或类似的内容结尾。
如果需要,您可以执行以下操作(单线):
FOR /F "usebackq delims=;" %A IN (`dir %sourcePath% /B /AD ^|FINDSTR /V "%destinationPath%"`) DO @xcopy "%sourcePath%\%A\*.*" "%destinationPath%\" /EK
如果源中充满了您应该在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
希望这对您有帮助! :-)
再见