By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,011 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Dec 16 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
You mean like XCOPY?

" **Developer**" <RE*************@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Dec 16 '05 #2

P: n/a
I'm sorry, I meant programatically.
Thanks
"bill" <be****@datamti.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
You mean like XCOPY?

" **Developer**" <RE*************@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Dec 16 '05 #3

P: n/a
Open VS Help and search on File Class.

Dec 16 '05 #4

P: n/a
> I'm sorry, I meant programatically.
Thanks


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
Dec 16 '05 #5

P: n/a
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.

<za***@construction-imaging.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Open VS Help and search on File Class.

Dec 16 '05 #6

P: n/a
Yes, I've used XCOPY since DOS.
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I'm sorry, I meant programatically.
Thanks
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

Cor


Thanks for the implied suggestion.
Dec 16 '05 #7

P: n/a
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
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
Attached is some code that copies one directory to another recursively so
as
to include subfolders. Hopefully it will get you started.

Greg

Dec 16 '05 #8

P: n/a
> 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
Dec 16 '05 #9

P: n/a
> 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



Dec 16 '05 #10

P: n/a
" **Developer**" <RE*************@a-znet.com> schrieb:
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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 16 '05 #11

P: n/a
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]


" **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
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
Attached is some code that copies one directory to another recursively so
as
to include subfolders. Hopefully it will get you started.

Greg


Dec 16 '05 #12

P: n/a
" **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

Dec 16 '05 #13

P: n/a
Addendum:

Recursive solution for .NET 1.0/1.1:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/76d895c77295adb8>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 16 '05 #14

P: n/a
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

Dec 17 '05 #15

P: n/a
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


Dec 17 '05 #16

P: n/a
Bill,
Cor showed me that XCOPY can be used programmatically.

Thanks for suggesting it - sorry I didn't understand at first.
" **Developer**" <RE*************@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I'm sorry, I meant programatically.
Thanks
"bill" <be****@datamti.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
You mean like XCOPY?

" **Developer**" <RE*************@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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



Dec 18 '05 #17

P: n/a

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP09.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.

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

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


Dec 18 '05 #18

P: n/a
> what is difficult in above code ????


Nothing, now that I've seen it I can read the help about the methods you
called.

Thanks
Dec 18 '05 #19

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uu*************@TK2MSFTNGP15.phx.gbl...
" **Developer**" <RE*************@a-znet.com> schrieb:
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.

Not yet but maybe some day.

Thanks
Dec 18 '05 #20

P: n/a
Thanks, I got it.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Addendum:

Recursive solution for .NET 1.0/1.1:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/76d895c77295adb8>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 18 '05 #21

P: n/a
"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....


Not sure about your email reader, but stupid Outlook Express blocks most
attachments by default.

Easy enough to fix, if you know what the problem is:

Tools->Options->Security Tab->Uncheck the "do not allow attachments..."

Greg
Dec 18 '05 #22

P: n/a
" **Developer**" <RE*************@a-znet.com> schrieb:
Thanks, I got it.


BTW: I recommend these solutions over using 'XCOPY' :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Dec 18 '05 #23

P: n/a
>
BTW: I recommend these solutions over using 'XCOPY' :-).


Me too (before it is misunderstood), therefore I have written in every
message in this thread. Withouth any control about the proces.

:-)

Cor
Dec 18 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.