hello Scorpion ...
this was Gregs example
Option Strict On
Imports System.IO
Imports System.Configuration
Module Module1
Sub Main()
Dim Source As String = ConfigurationSettings.AppSettings("source")
Dim Dest As String = ConfigurationSettings.AppSettings("destination") & "\"
& Year(Today) & Right("0" & Month(Today), 2) & Right("0" & Day(Today), 2)
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.")
' Flush and close the output stream.
fileLog.Flush()
fileLog.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 aDirs() As String
Dim aFiles() As String
Dim ok As Boolean = True
Trace.WriteLine("Inspecting folder " & sourceDir)
Try
' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)
For Each folderpath As String In aDirs
Dim sDir As String
' Get the path of the source directory.
sDir = System.IO.Path.GetFileName(folderpath)
' Create the new directory in the destination directory.
System.IO.Directory.CreateDirectory(System.IO.Path .Combine(destDir, sDir))
' Since we are in recursive mode, copy the children also
ok = RecursiveCopyFiles(folderpath, System.IO.Path.Combine(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 Each filepath As String In aFiles
Dim sFile As String
' Get the full path of the source file.
sFile = System.IO.Path.GetFileName(filepath)
Try
' Copy the file.
Trace.WriteLine("Copying " & filepath)
System.IO.File.Copy(filepath, System.IO.Path.Combine(destDir, sFile))
Try
' Delete the file.
Trace.WriteLine("Deleting " & filepath)
System.IO.File.Delete(filepath)
Catch ex As Exception
Trace.WriteLine("Error deleting " & filepath)
Trace.WriteLine(ex.Message)
ok = False
End Try
Catch ex As Exception
Trace.WriteLine("Error copying " & filepath)
Trace.WriteLine(ex.Message)
ok = False
End Try
Next
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
And this was mine in case you missed it
Public Sub CopyDir(ByVal Src As String, ByVal Dst As String)
Dim Files() As String, Element As String
If Microsoft.VisualBasic.Right(Dst, Dst.Length - 1) <>
Path.DirectorySeparatorChar Then
Dst &= Path.DirectorySeparatorChar
End If
If Not Directory.Exists(Dst) Then Directory.CreateDirectory(Dst)
Files = Directory.GetFileSystemEntries(Src)
For Each Element In Files
If Directory.Exists(Element) Then
CopyDir(Element, Dst & Path.GetFileName(Element))
Else
File.Copy(Element, Dst & Path.GetFileName(Element), True)
End If
Next Element
End Sub
the above code works in Both VS.Net 2003 and in VS.net 2005 ( maybe also in
previous versions but uhhmm don`t have that on my comp anymore )
however as Herfried already suggested you can use in VS.Net 2005
My.Computer.FileSystem.CopyDirectory()
regards
Michel Posseth [MCP]
"scorpion53061" <sc************@nospamhereyahoo.com> wrote in message
news:OF**************@TK2MSFTNGP15.phx.gbl...
Greg,
Could you post your sample as a zip or text? Email reader wont let me
download your file....
Thanks,
Kelly
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:Oo*************@TK2MSFTNGP11.phx.gbl..." **Developer**" <RE*************@a-znet.com> wrote in message
news:uM**************@TK2MSFTNGP15.phx.gbl... Great on two accounts:
1) Answers my question: No there isn't an easy way!
2) Gives me a general solution that I can use.
Thanks a lot
I prefer the recursive code method, rather than some of the other methods
suggested, soley because of the my need to log errors when individual
files failed to copy.
The best solution (or easiest) really depends on your specific needs.
BTW, my code is moving a folder (and subfolders/files) from one location
to another Hence, the delete stuff. Modify as needed.
Greg