how to move a directory between two different volumes

  • Thread starter Thread starter Karel
  • Start date Start date
K

Karel

Hello,

I have a VB.NET application where I want to move directories over a
network.

I tried this with system.io.directory.move, but that doesn't work over
different volumes.
Has anyone a sollution for this problem?

thx

Karel
 
One solution is to map a drive and use the IO Filesystem classes to copy the
data. Ther's probably a slicker way to do it, but this will work. In the
example below, the letter H: is mapped to a URN resource newpublic on the
server \\tower1. This drive letter can then be used as a local drive letter
would be.

IE.
Try
System.Diagnostics.Process.Start("net", "use h:
\\tower1\newpublic")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try


Regards - OHM ( Terry Burns )
 
I ended up mapping a drive like OHM recommended. Here is some code from a
console app of mine to get you started... (it also creates a trace file to
let you know when something goes wrong)

HTH,
Greg
------------------------------------------
Option Strict On

Imports System.IO

Module Module1

Sub Main()

Dim Source As String = "Z:\ilc\temp"
Dim Dest As String = "N:\temp\" & Year(Today) & Right("0" &
Month(Today), 2) & Right("0" & Day(Today), 2)

System.IO.Directory.CreateDirectory(Dest)

'get the log file name
Dim logFileName As String = Dest & "\trace.log"

'open the log file

Dim fileLog As StreamWriter = File.CreateText(logFileName)

'define the log file trace listener
Dim logListener As TextWriterTraceListener = New
TextWriterTraceListener(fileLog)

'add the new trace listener to the collection of listeners
Trace.Listeners.Add(logListener)

Dim consoleListener As MyTrace = New MyTrace
Trace.Listeners.Add(consoleListener)

'make sure that we actually write the data out
Trace.AutoFlush = True

RecursiveCopyFiles(Source, Dest, True)

Trace.WriteLine("Finished.")

logListener.Flush()
logListener.Close()

System.Environment.ExitCode = 0



End Sub



' Recursively copy all files and subdirectories from the
' specified source to the specified destination.
Private Function RecursiveCopyFiles( _
ByVal sourceDir As String, _
ByVal destDir As String, _
ByVal bTop As Boolean) As Boolean

Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String
Dim ok As Boolean = True

' Add trailing separators to the supplied paths if they don't exist.
If Not
sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

Trace.WriteLine("Inspecting folder " & sourceDir)

Try
' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For i = 0 To aDirs.GetUpperBound(0)

' Get the position of the last separator in the current
path.
posSep = aDirs(i).LastIndexOf("\")

' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length -
(posSep + 1))

' Create the new directory in the destination directory.
System.IO.Directory.CreateDirectory(destDir + sDir)

' Since we are in recursive mode, copy the children also
ok = True
ok = RecursiveCopyFiles(aDirs(i), (destDir + sDir), False)

If ok Then
Try
Trace.WriteLine("Deleting " & destDir + sDir)
System.IO.Directory.Delete(destDir + sDir)
Catch ex As Exception
Trace.WriteLine("Error deleting " & destDir + sDir)
Trace.WriteLine(ex.Message)
ok = False
End Try
End If
Next
Catch ex As Exception
Trace.WriteLine("Error reading directory " & sourceDir)
End Try



' Get the files from the current parent.

aFiles = System.IO.Directory.GetFiles(sourceDir)


' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)

' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")

' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length -
(posSep + 1))

Try
' Copy the file.
Trace.WriteLine("Copying " & aFiles(i))
System.IO.File.Copy(aFiles(i), destDir + sFile)

Try
' Delete the file.
Trace.WriteLine("Deleting " & aFiles(i))
System.IO.File.Delete(aFiles(i))
Catch ex As Exception
Trace.WriteLine("Error deleting " & aFiles(i))
Trace.WriteLine(ex.Message)
ok = False
End Try

Catch ex As Exception
Trace.WriteLine("Error copying " & aFiles(i))
Trace.WriteLine(ex.Message)
ok = False
End Try

Next i

If Not bTop Then
Try
Trace.WriteLine("Deleting folder " & sourceDir)
System.IO.Directory.Delete(sourceDir)
Catch ex As Exception
Trace.WriteLine("Error deleting folder " & sourceDir)
Trace.WriteLine(ex.Message)
ok = False
End Try
End If

End Function


End Module


' disallow inheriting this class
Public NotInheritable Class MyTrace
Inherits TraceListener

' disallow instantiation
Public Sub New()
MyBase.New()
End Sub

<Conditional("TRACE")> _
Public Overloads Overrides Sub Write(ByVal message As String)
Console.Write(message)
End Sub

<Conditional("TRACE")> _
Public Overloads Overrides Sub WriteLine(ByVal message As String)
Console.WriteLine(message)
End Sub
End Class
 
Greg Burns said:
I ended up mapping a drive like OHM recommended. Here is some code from a
console app of mine to get you started... (it also creates a trace file to
let you know when something goes wrong)

HTH,
Greg

thx greg,

your example helped me a lot!!
Just what I searched for

Karel
 
Back
Top