 |
Anita Level: Big Cheese
 Registered: 19-05-2005 Posts: 24
|
Copy files
I am trying to copy files from one computer on a network to another computer on a network. Using VB .Net. I see a file and directory object in the system.io namespace. But I don't understand how to copy several files within a folder using these objects. Can someone help me with this. Thanks!
|
|
19-05-2005 at 06:45 PM |
|
|
humberto Level: VB Lord
 Registered: 13-01-2005 Posts: 246
|
Re: Copy files
' Recursively copy all files and subdirectories from the
' specified source to the specified destination.
Private Sub RecursiveCopyFiles( _
ByVal sourceDir As String, _
ByVal destDir As String, _
ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String
' Add trailing separators to the supplied paths if they don't exist.
If Not sourceDir.EndsWith(System.IO.path.DirectorySeparatorChar.ToString()) Then
sourceDir& = System.IO.path.DirectorySeparatorChar
End If
If Not destDir.EndsWith(System.IO.path.DirectorySeparatorChar.ToString()) Then
destDir& = System.IO.path.DirectorySeparatorChar
End If
' Recursive switch to continue drilling down into dir structure.
If fRecursive Then
' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)
For i = 0 To aDirs.GetUpperBound(0)
' Get the position of the last separator in the current path.
posSep = aDirs(i).LastIndexOf("\")
' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length - (posSep + 1))
' Create the new directory in the destination directory.
System.IO.Directory.CreateDirectory (destDir + sDir)
' Since we are in recursive mode, copy the children also
RecursiveCopyFiles(aDirs(i), (destDir + sDir), fRecursive)
Next
End If
' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)
' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)
' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")
' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length - (posSep + 1))
' Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)
Next i
End Sub
|
|
|
20-05-2005 at 11:44 AM |
|
|
Anita Level: Big Cheese
 Registered: 19-05-2005 Posts: 24
|
Re: Copy files
This code is cool, but it looks like it copies all the files in all directories from the source path to the destination. But perhaps I don't fully understand the code.
What if I want to copy only the files in the last folder of the source path?
Thanks!
|
|
20-05-2005 at 02:36 PM |
|
|
Anita Level: Big Cheese
 Registered: 19-05-2005 Posts: 24
|
Re: Copy files
Great, now I get it. Thanks!
|
|
20-05-2005 at 03:03 PM |
|
|
Anita Level: Big Cheese
 Registered: 19-05-2005 Posts: 24
|
Re: Copy files
This works really great, but now I want to copy only a directory and files in the last directory of the path. For example,
"c:\temp\filesanddir"
Filesanddir contains some files and another dir with files. I only want to copy the directory and files that are in filesanddir and not any files or dirs in temp.
Can I tweak the code to do this? Thanks.
|
|
24-05-2005 at 07:55 PM |
|
|
Anita Level: Big Cheese
 Registered: 19-05-2005 Posts: 24
|
Re: Copy files
Well, after further testing, it looks like the code already is doing what I want. So, that's great.
How can I make the code create a destination directory if one does not exist. For example, if the source path is
c:\temp\filesanddir
and the destination is
c:\temp
Thanks for your help!
|
|
24-05-2005 at 08:37 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Copy files
Directory Class: System.IO Namespace
Directory.Exists (Path) - check if directory exists
Directory.CreateDirectory (Path) - creates new directory
[Edited by Goran on 25-05-2005 at 06:30 PM GMT]
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
25-05-2005 at 05:08 PM |
|
|
|
|
 |
 |