473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to move a directory between two different volumes

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
Nov 20 '05 #1
3 4151
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 )

"Karel" <ka***********@informat.be> wrote in message
news:68**************************@posting.google.c om...
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

Nov 20 '05 #2
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.DirectorySeparat orChar.ToString()) Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.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
"Karel" <ka***********@informat.be> wrote in message
news:68**************************@posting.google.c om...
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

Nov 20 '05 #3
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message news:<ut**************@TK2MSFTNGP12.phx.gbl>...
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
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kamus of Kadizhar | last post by:
Thanks to everyone on this list. I now have a functioning piece of python code!! :-)) Now I'm trying to clean it up. I have the same (or similar) lines repeated several times: ...
1
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ...
3
by: andrew | last post by:
I do not understand how this function should work. It seems to be different than when I drag a folder ... error message: Cannot create a file when that file already exists. c:\atest has...
1
by: rv | last post by:
I am developing an ASP.NET website with C#, VS 2003 and Framework 1.1. For some reason every time I execute a Directory.Move command, the next command takes 10-20 seconds to execute. Has anyone...
8
by: Ron Vecchi | last post by:
I know this has been asked before but I out of all the posts I've read regarding using Directory.Move and then the application restarting itself I have found no answer except that Sessions are...
15
by: Jameson | last post by:
Happy New Year, Everyone! I am trying to figure out how to display a bunch of images (mainly JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a local directory on the system....
3
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot...
8
by: Lee | last post by:
guys, I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will...
1
by: Madhu Harchandani | last post by:
Hello i am working in a project in which i am using xnat technology in that data is entered through xml files only i need to enter 1000 records .so it is very cumbersome to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.