Is there an easy way to copies all files in a directory into another directory?

D

**Developer**

Is there an easy way to copies all files in a directory into another
directory?


What about coping subdirectories too?



Thanks in advance for any info
 
C

Cor Ligthert [MVP]

I'm sorry, I meant programatically.

And you asked an easy way and the answer was XCOPY. Did you try it?

(You did not ask for the most controlled way)

Cor
 
D

**Developer**

I've used the File class before but never to copy an entire folder contents.

Just looked again and do not see a method for copying entire folder.
Am I missing something or are you suggesting I use the Copy method to copy
the files one at a time?


Thanks

PS

I'm presently using the FileSystem's FileCopy method to copy them one at a
time and am wondering if there is a better way.
 
D

**Developer**

Yes, I've used XCOPY since DOS.


Cor Ligthert said:
And you asked an easy way and the answer was XCOPY. Did you try it?
Yes, I've used XCOPY since DOS. And always liked it.
(You did not ask for the most controlled way)

What is the most controlled way to copies all files in a directory into
another directory?


What about coping subdirectories too?



Thanks in advance for any info



Thanks for the implied suggestion.
 
D

**Developer**

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
 
C

Cor Ligthert [MVP]

Yes, I've used XCOPY since DOS.Than why not try it. It is not nice as I wrote and withouth any control,
however it goes and easy. You can hide the Dos window if you want.

\\\
Dim p As New Process
p.StartInfo.Arguments = "C:\Test1 C:\TestA\"
p.StartInfo.FileName = "xcopy"
p.Start()
///

I hope this helps,

Cor
 
C

Cor Ligthert [MVP]

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.

You got from Bill and easy solution 7 minutes after that you placed your
question here.

The way you replied on that did him probably not give you any more
information.

I asked again why it did not fit, and got a same kind as answer as you gave
to Bill.

In my opinion not the best way to get some help in a newsgroup.
However feel free to continue that.

By the way the message I have sent with the complete Xcopy code was before I
had read you message I am answering now.

Cor
 
H

Herfried K. Wagner [MVP]

**Developer** said:
Is there an easy way to copies all files in a directory into another
directory?

What about coping subdirectories too?

If you are using VB 2005, 'My.Computer.FileSystem.CopyDirectory' should do
the trick.
 
M

m.posseth

Huh ????

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

what is difficult in above code ????



i believe it is a definite yes to 1 and 2 of your goals :)

regards

and happy coding

Michel Posseth [MCP]
 
G

Greg Burns

**Developer** said:
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
 
S

scorpion53061

Greg,

Could you post your sample as a zip or text? Email reader wont let me
download your file....

Thanks,
Kelly
 
M

m.posseth

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]
 
D

**Developer**

Bill,
Cor showed me that XCOPY can be used programmatically.

Thanks for suggesting it - sorry I didn't understand at first.
 
D

**Developer**

Cor Ligthert said:
You got from Bill and easy solution 7 minutes after that you placed your
question here.

I didn't recognize it as such then.

I'm afraid I don't understand what your telling me below but your other post
made me understand Bill's reply.

Thanks
 
D

**Developer**

what is difficult in above code ????
Nothing, now that I've seen it I can read the help about the methods you
called.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top