To create the ability to copy directory’s across multiple servers. E.g. copy directory A from Server: 1 to directory B on server 2.
When copying the directory’s you will only want to copy across new and changed files and for the changed files to overwrite any files on Server: 2
Another possibility is that you do not want to copy all files/directories but exclude certain files.
2 different ways of doing this 1 being the Clean purely .net way and the other and the one that I have chosen is a legacy dos command within a VB class and form.
Using ROBOCOPY we are reducing the amount of code needed whilst having the complex features that we require for this purpose.
First thing that I do is to turn on
Option Strict and Option Explicit On
We then need to import any libraries that we are going to use which are:
Imports System.IO Imports System.Diagnostics
Now we create our classes and functions:
Public Class FileDirectoryCopy Private _psi As ProcessStartInfo Private _cmd As Process Shared Function CopyDirectoryXtoY(ByVal x As String, ByVal y As String) As Boolean Return CopyDirectoryXtoY(x, y, String.Empty) End Function Shared Function CopyDirectoryXtoY(ByVal x As String, ByVal y As String, ByVal excludedTypes As String) As Boolean 'Dim result As Boolean Dim _cmd As Process Dim _psi As New ProcessStartInfo(x, y) _psi.UseShellExecute = False _psi.RedirectStandardOutput = True _cmd = Process.Start(_psi) Dim sOutput As String Using oStreamReader As System.IO.StreamReader = _cmd.StandardOutput sOutput = oStreamReader.ReadToEnd() End Using Console.WriteLine(sOutput) End Function End Class
So what’s happening above is that the class is passed its x and y parameters which in my case is going to be ("ROBOCOPY", """Directory 1""Directory 2"" /MIR /XF .Config ")
The /MIR and /XF are the switches for Robocopy. /MIR is the command that copies the directory and overwrites any files that are needed.
The /XF is the exclude command and in my case it is excluding and .config files.
Obviously we will need to have the program us create an output so we can see what it has done and that is what we have written above with the code that RedirectStandardOutput = True is doing.
So if you were to put a break point on the line
Console.WriteLine(sOutput)and run your program you can see what it has done i.e if it has copied the data or if it hasn’t liked your parameters.
So that’s it a quick way to create a simple Directory Copy Program with limited amount of coding using DOS.
The other options instead of the Legacy Dos option i have provided are:
the framework way or just looping through all the files in the directory
Dim fooDirectory As String = "" Dim fooDirectoryDest As String = "" If Not IO.Directory.Exists(fooDirectoryDest) Then IO.Directory.CreateDirectory(fooDirectoryDest) If fooDirectoryDest(fooDirectoryDest.Length-1)!= "\" then fooDirectoryDest = fooDirectoryDest + "\" Dim files() As String = IO.Directory.GetFiles(fooDirectory) For Each temp As String In files IO.File.Copy(temp, fooDirectoryDest + IO.Path.GetFileName(temp)) Next
This post has been edited by JackOfAllTrades: 31 October 2012 - 06:03 AM
Reason for edit:: Fixed code tags