I want to copy a folder structure from one directory to another and keep the folder permissions within the structure. Is there code to use robocopy for this?

You could spawn it as a separate process with the same arguments you would use on the command line. You can also redirect stdout and stderr and capture it from within your app and display the text in a control. Do you need an example of how to do this?

Yes please can you show me an example of how to spawn it as a sepereate process?

I've attached a sample robocopy project. I redirect StdOut so I can capture and display it. You can also do the same for StdErr. Note that robocopy (in the command line) rewrites the current line to display percentages. You'll have to decide how to handle this when you display in a textbox.

You cannot write to a control in the main form from a background thread so we use a delegate.

Feel free to ask more questions.

Thank you for the code Jim. I appreciate it. After following your code it still does not want to copy the folders. I posted my code below. Do you see anything that looks wrong?

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim srce As String = "H:\##ISDEPT\Folder Templates\a"
        Dim dest As String = "H:\VBTEST\00010"
        Dim MyProcess As Process

        If Not My.Computer.FileSystem.DirectoryExists(srce) Then
            MsgBox("the source folder does not exist")
            Exit Sub
        End If

        If Not My.Computer.FileSystem.DirectoryExists(dest) Then
            MsgBox("the destination folder does not exist")
            Exit Sub
        End If

        MyProcess = New Process

        With MyProcess.StartInfo
            .FileName = "H:\##ISDEPT\Folder Templates\robocopy.exe"
            .Arguments = srce & " " & dest & " /e /r:0"
            .UseShellExecute = False
            .CreateNoWindow = True
            .RedirectStandardInput = False
            .RedirectStandardOutput = True
            .RedirectStandardError = True
        End With

        MyProcess.Start()
        MyProcess.BeginOutputReadLine()

    End Sub
End Class

MyProcess goes out of scope as soon as the button_click handler exits. You took out some critical code such as the declaration of the delegate, the sub to handle the processing of stdout, etc. What happens when you run the code that I gave you? If it runs as is, then fails when you change something that is an indication that you made the wrong change. I tested the code before I posted it and it worked just fine.

ALright yes i see what I needed to add back in. Is there anyone to modify your code so that it retains the file permissions? The copy removed some people from the security permissions

Just add more arguments to the list for robocopy

.Arguments = srce & " " & dest & " /e /r:0 /COPY:DATSO"

Please mark this as solved if this is what you needed.

Edited 3 Years Ago by Reverend Jim

Good day Jim, I'm interested with doing this but your attachment is not working with my current version of visual studio. I'm using 2005.

I'm running Visual Studio 2010 so I don't know what you would need to change. Any reason you can't download and install VS 2010?

This article has been dead for over six months. Start a new discussion instead.