473,505 Members | 15,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
23 2395
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
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
Open VS Help and search on File Class.

Dec 16 '05 #4
> 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
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
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
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
> 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
> 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
" **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
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
" **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
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
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
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
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

"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
> 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

"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
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
"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
" **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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2187
by: JW | last post by:
I'm displaying product thumbnails with brief descriptions on web pages. Clicking on the product does a javascript popup with larger image and detailed description info passed to the javascript...
1
7101
by: jajoo | last post by:
Hi everyone, I am trying to send files with multipart/form-date. Everything is ok with the send. But when I am receiving the files I should specify a directory where the files to be saved. The...
9
3188
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
8
1925
by: Adam Clauss | last post by:
I have a folder containing many subfolders (and subfolders and....) all containing various .cs files. Is there any "easy" way to get them all added to the solution. Preferable would be that the...
3
1306
by: Rob Meade | last post by:
Hi all, I have created 7 web custom controls for 7 sections of our template. I have them currently on my local pc - where vs placed them...the plan was that I would create these - throw them...
18
2272
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or...
2
2597
by: BLetts | last post by:
I have an ASP.NET app that must allow users to upload files. The files are stored in a virtual directory. I use Server.MapPath to map from the virtual directory name to a physical path, then the...
2
1945
by: Chris Ashley | last post by:
I've written a C++ Wrapper DLL which I need to add a reference to in my ASP.Net project. This in turn is dependent on the DLL it wraps which is in turn dependent on some other DLLs. When I...
15
1399
by: Andy B | last post by:
I need to count files in a certain directory that has the string -Contract Template.xml at the end of it. How would I do this?
0
7218
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
7307
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
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
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
7478
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...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.