-1

I want to copy a folder with xcopy. However, as soon as I place the destination folder in the source folder, nothing is being copied anymore. Is there a workaround for this problem?

The idea is to generate a backup of an entire folder strucutre (source) into one subfolder. When executing xcopy I exclude the subfolder for the backup (destination), where my backups should be stored.

I have already tested my code and it works just fine as long as the destination folder does no lie within the source folder.

The code is written with 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

The code is being executed with no error, but when I check the destination folder, no files haven been copied.

Dominique
  • 16,450
  • 15
  • 56
  • 112
MarkusNY
  • 13
  • 5

1 Answers1

0

this should not be possible since it should end with the error: "unable to execute a cyclic copy" or stuff like that.

if you want you can do something like this (one-liner):

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

if the SOURCE is filled with directories that you should copy in the DEST. subdirectory with all their files.

Pay Attention to:

 ... %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

Hope this helps you! :-)

Bye

ienaxxx
  • 1,292
  • 1
  • 8
  • 7