Copy Folder

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

Hi all,

I am pretty brand new to VB.Net, but I want to begin getting away from
VB6 and start using VB.Net for some of my more simple apps to gain some
experience.
I need some help on what is probably a very simple program. I would
like to create a form where I specify a source drive and folder and target
drive and folder, and the app copies the contents of the source to the
target. Essentially, copy a folder from one spot to another.
Looking through the MSDN on .Net classes, I see there is both a
System.IO.Directory and System.IO.File class, yet only the System.IO.File
has a Copy method. I suppose I could try to use the GetDirectories and
GetFiles methods of the System.IO.Directory class to iterate through the
subfolders and files in each subfolder within the source folder, and then
use the Copy method of the System.IO.File to copy the files over....but that
seems like I would need to do a lot of looping since I may or may not know
how many sub-levels the app will encounter in any given folder.
So, I am asking for a little help, a pointer or two, a link to an
article, something, that will help me create this (what would seem like)
simple app. Your feedback is appreciated!

-- Andrew
 
* "Andrew said:
I am pretty brand new to VB.Net, but I want to begin getting away from
VB6 and start using VB.Net for some of my more simple apps to gain some
experience.
I need some help on what is probably a very simple program. I would
like to create a form where I specify a source drive and folder and target
drive and folder, and the app copies the contents of the source to the
target. Essentially, copy a folder from one spot to another.
Looking through the MSDN on .Net classes, I see there is both a
System.IO.Directory and System.IO.File class, yet only the System.IO.File
has a Copy method. I suppose I could try to use the GetDirectories and
GetFiles methods of the System.IO.Directory class to iterate through the
subfolders and files in each subfolder within the source folder, and then
use the Copy method of the System.IO.File to copy the files over....but that
seems like I would need to do a lot of looping since I may or may not know
how many sub-levels the app will encounter in any given folder.

Move:
'System.IO.Directory.Move'.

Copy:
P/invoke on 'SHFileOperation':
<http://www.palmbytes.de/content/dotnetmisc/dl/shellfileoperation.zip>.
 
Henfried,

Thanks for your response, but two things here.... One, I can neither
speak nor read German, so the page you gave me a link to I do not
understand, and second, I want to Copy files, not Move them. Still, thanks
for your input.

-- Andrew
 
* "Andrew said:
Thanks for your response, but two things here.... One, I can neither
speak nor read German, so the page you gave me a link to I do not

Just ignore the text. The problem is, that there is no managed way to
simply copy a folder with all its subfolders and files. You already
described the way to loop through all of the folders and copy the files,
but that's not very comfortable.

Windows provides a function 'SHFileOperation' which simplyfies copying a
folder and its subfolders. The German language link included a sample
for using this function in C#. I feel sorry, but I don't have a link to
a VB.NET implementation at the moment.
 
Hi Andrew,

You're going down the right path.

What you are trying to do is not too easy, because you have to run a
recursive routine to get all
the subs and the files they contain.

I wrote several routines in an effort to use this concept to save .net
solutions in order to provide backup copies. I won't bore you with all the
details; instead, below is the code that makes it happen, which I'm sure
you'll be able to follow along and work with:
Imports System.IO

Public Class Form1

Inherits System.Windows.Forms.Form

Public selectedsubs As New ArrayList()

Public subs2 As New ArrayList()



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim dirinfo As New DirectoryInfo("c:\vsapps\")

For Each dirinfo In dirinfo.GetDirectories()

solutionsbox.Items.Add(dirinfo.Name)

Next

End Sub

Private Sub saveit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles saveit.Click

Dim dirsys As Directory

Dim copytopath As String

Dim dirfullpath As String

Dim solselectionsarray As String

Dim mdirectory As DirectoryInfo

Dim mfile As FileInfo

Dim dfile As File()

Dim destinationfile As String

Dim solutionsboxfp As New ArrayList()

Dim shortstring, stringfront As String

Dim i, j, startpos As Integer

Me.Cursor = Cursors.WaitCursor

For Each solselectionsarray In solutionsbox.CheckedItems

selectedsubs.Add("c:\vsapps\" & solselectionsarray)

Next

subs2.Clear()

For i = 0 To selectedsubs.Count - 1

mdirectory = New DirectoryInfo(selectedsubs(i))

listdirectories(mdirectory)

Next

For j = 0 To subs2.Count - 1

mdirectory = New DirectoryInfo(subs2(j))

startpos = InStrRev(mdirectory.FullName, "\")

shortstring = Mid(mdirectory.FullName, startpos + 1)

stringfront = Mid(mdirectory.FullName, 11)

If dirsys.Exists("c:\dotnetsource\" & stringfront) Then

Else

dirsys.CreateDirectory("c:\dotnetsource\" & stringfront)

End If

For Each mfile In mdirectory.GetFiles

destinationfile = "c:\dotnetsource\" & stringfront & "\" & mfile.Name

File.Copy(mfile.FullName, destinationfile, True)

Next

Next

Me.Cursor = Cursors.Default

Application.Exit()



End Sub

Sub listdirectories(ByVal thedirectory As DirectoryInfo)

Dim tempdir As DirectoryInfo

subs2.Add(thedirectory.FullName)

For Each tempdir In thedirectory.GetDirectories()

listdirectories(tempdir)

Next

End Sub

End Class



HTH,

Bernie Yaeger
 
Here's a quick down and dirty example of recursive looping, you'd want to
throw some exception handling in there and you might be able to tighten some
things up, but it should work. Just replace the two constants with the
appropriate folders.

Imports System.IO
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const FOLDER_FROM As String = "C:\ATI" ' Folder to copy from
Const FOLDER_TO As String = "H:\" 'Folder to create above folder in
Dim newDir As DirectoryInfo = Directory.CreateDirectory(FOLDER_TO &
"\" & New DirectoryInfo(FOLDER_FROM).Name)
CopyFiles(New DirectoryInfo(FOLDER_FROM), newDir)
End Sub
Private Sub CopyFiles(ByVal FolderFrom As DirectoryInfo, ByVal FolderTo
As DirectoryInfo)
Dim Files() As String = Directory.GetFiles(FolderFrom.FullName)
Dim SF() As String = Directory.GetDirectories(FolderFrom.FullName)
Dim Var As Integer
For Var = Files.GetLowerBound(0) To Files.GetUpperBound(0)
File.Copy(Files(Var), FolderTo.FullName & "\" & New
FileInfo(Files(Var)).Name)
Next
For Var = SF.GetLowerBound(0) To SF.GetUpperBound(0)
Dim dName As String = New DirectoryInfo(SF(Var)).Name
Dim newD As New DirectoryInfo(FolderTo.FullName & "\" & dName)
newD.Create()
CopyFiles(New DirectoryInfo(FolderFrom.FullName & "\" & dName),
newD)
Next
End Sub
 
Chris,

Thank you for your code sample. Taking this as a starting point (and
seeing what new functions and classes I needed to use) I have been able to
modify it, make some adjustments, add error trapping, etc., and make a
viable little app to help me backup my files to my CD+R/W drive.
You da man, thanks!

-- Andrew
 
Back
Top