473,765 Members | 1,966 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
23 2435
" **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.Fi leSystem.CopyDi rectory' 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.Visua lBasic.Right(Ds t, Dst.Length - 1) <>
Path.DirectoryS eparatorChar Then

Dst &= Path.DirectoryS eparatorChar

End If

If Not Directory.Exist s(Dst) Then Directory.Creat eDirectory(Dst)

Files = Directory.GetFi leSystemEntries (Src)

For Each Element In Files

If Directory.Exist s(Element) Then

CopyDir(Element , Dst & Path.GetFileNam e(Element))

Else

File.Copy(Eleme nt, Dst & Path.GetFileNam e(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******** ******@TK2MSFTN GP15.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*******@news groups.nospam> wrote in message
news:u1******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP15.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.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/76d895c77295adb 8>

--
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*******@news groups.nospam> wrote in message
news:Oo******** *****@TK2MSFTNG P11.phx.gbl...
" **Developer**" <RE************ *@a-znet.com> wrote in message
news:uM******* *******@TK2MSFT NGP15.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.Configur ation

Module Module1

Sub Main()

Dim Source As String = ConfigurationSe ttings.AppSetti ngs("source")

Dim Dest As String = ConfigurationSe ttings.AppSetti ngs("destinatio n") & "\"
& Year(Today) & Right("0" & Month(Today), 2) & Right("0" & Day(Today), 2)

Directory.Creat eDirectory(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 TextWriterTrace Listener = New
TextWriterTrace Listener(fileLo g)

'add the new trace listener to the collection of listeners

Trace.Listeners .Add(logListene r)

Dim consoleListener As MyTrace = New MyTrace

Trace.Listeners .Add(consoleLis tener)

'make sure that we actually write the data out

Trace.AutoFlush = True

RecursiveCopyFi les(Source, Dest, True)

Trace.WriteLine ("Finished." )

' Flush and close the output stream.

fileLog.Flush()

fileLog.Close()

System.Environm ent.ExitCode = 0

End Sub

' Recursively copy all files and subdirectories from the

' specified source to the specified destination.

Private Function RecursiveCopyFi les( _

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.Direc tory.GetDirecto ries(sourceDir)

For Each folderpath As String In aDirs

Dim sDir As String

' Get the path of the source directory.

sDir = System.IO.Path. GetFileName(fol derpath)

' Create the new directory in the destination directory.

System.IO.Direc tory.CreateDire ctory(System.IO .Path.Combine(d estDir, sDir))

' Since we are in recursive mode, copy the children also

ok = RecursiveCopyFi les(folderpath, System.IO.Path. Combine(destDir , sDir),
False)

If ok Then

Try

Trace.WriteLine ("Deleting " & destDir & sDir)

System.IO.Direc tory.Delete(des tDir & 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.Direc tory.GetFiles(s ourceDir)

' 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(fil epath)

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.Direc tory.Delete(sou rceDir)

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("T RACE")> _

Public Overloads Overrides Sub Write(ByVal message As String)

Console.Write(m essage)

End Sub

<Conditional("T RACE")> _

Public Overloads Overrides Sub WriteLine(ByVal message As String)

Console.WriteLi ne(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.Visua lBasic.Right(Ds t, Dst.Length - 1) <>
Path.DirectoryS eparatorChar Then

Dst &= Path.DirectoryS eparatorChar

End If

If Not Directory.Exist s(Dst) Then Directory.Creat eDirectory(Dst)

Files = Directory.GetFi leSystemEntries (Src)

For Each Element In Files

If Directory.Exist s(Element) Then

CopyDir(Element , Dst & Path.GetFileNam e(Element))

Else

File.Copy(Eleme nt, Dst & Path.GetFileNam e(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.Fil eSystem.CopyDir ectory()

regards

Michel Posseth [MCP]


"scorpion53 061" <sc************ @nospamhereyaho o.com> wrote in message
news:OF******** ******@TK2MSFTN GP15.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*******@news groups.nospam> wrote in message
news:Oo******** *****@TK2MSFTNG P11.phx.gbl...
" **Developer**" <RE************ *@a-znet.com> wrote in message
news:uM****** ********@TK2MSF TNGP15.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 programmaticall y.

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

" **Developer**" <RE************ *@a-znet.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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******** ********@TK2MSF TNGP09.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******** *****@TK2MSFTNG P15.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.Fi leSystem.CopyDi rectory' should do
the trick.

Not yet but maybe some day.

Thanks
Dec 18 '05 #20

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

Similar topics

6
2206
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 functions from the table data. Products can change frequently, and I want to make the maintenance of this info easier by non-techies (each product has an image name, image width (currently not using getimagesize), a short description, and a long...
1
7131
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 problem is that Tomcat saves the files into %CATALINA_HOME%/bin directory. I am wondering why? Why not into my application directory? The application directory is %CATALINA_HOME%/webapps/picture. As a parameter of directory I am giving "." – which...
9
3204
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
8
1936
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 folders are actually created in the Solution Explorer so that I can find things easily. Its easy to select multiple files out of a single folder, but not recursively into subfolders. Any ideas? -- Adam Clauss
3
1318
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 into a central location then the rest of our team would be able to use them to create our generic template for our applications etc... One thing I've noticed is when I opened up a new webform and created my
18
2303
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 JPG files. The problem as I see it - if you know the name of the file, you could download it off the server (currently we are using an HTTP/Get but I'm going to be using WebClient in the new version.) If there any way to password protect the...
2
2613
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 FileUpload control SaveAs method to upload and save the file. This all works fine when the virtual directory points to a local drive on the ASP.NET server. However, when pointing to a remote drive (which is what my client wants to do), I get...
2
1953
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 reference the wrapper DLL VS2005 copies it to the 'bin' directory and I am unable to compile with the error 'the specified module could not be found'. It still has the same behaviour if I copy all the dependent DLLs to my bin directory, but this isn't a...
15
1418
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
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9955
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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 we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.