|
-
Nov 2nd, 2005, 03:51 PM
#1
Thread Starter
Junior Member
I need a basic line of code to copy a folder/directory
Hi, I am making a program where it needs to copy a folder/directory and move it. I have the code to do this with a file, but I was wondering if there's a similar code to copy a folder. I intend to copy/move the folder, then delete it from the original path with the kill command. Here's what I'm currently using for files, a similar line to move folders would be excellent.
FileCopy("C:\path1", "C:\path2")
Kill("C:\path1")
Thanks for any input!
-
Nov 2nd, 2005, 04:05 PM
#2
Fanatic Member
Re: I need a basic line of code to copy a folder/directory
The only thing I know that you can do is to create a sub routine that moves the files and then call that from a routine that loops through the folders you want to move. I don't know of a command to just move folders. I think you have to write the logic to do this yourself.
-
Nov 2nd, 2005, 05:08 PM
#3
Lively Member
Re: I need a basic line of code to copy a folder/directory
If you are going to delete the folder after you have copied it you could use the following:
VB Code:
System.IO.Directory.Move(sourcedir, targetdir)
or this is what I use to copy all the files and folders
VB Code:
Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Private Sub Copy_Info()
Dim strFolders() As String = IO.Directory.GetDirectories(Me.txtSourceLocation.Text)
Dim NewPath As String = strFullAccountTarget
Dim strFileExt As String
Dim strFileExt1 As String
'Get all the files in the main Directory
Dim MainFiles() As String = IO.Directory.GetFiles(Me.txtSourceLocation.Text)
Dim FileToCopy As String
'For each file in Source Directory copy to Target Directory
For Each FileToCopy In MainFiles
Dim f As IO.FileInfo = New IO.FileInfo(FileToCopy)
strFileExt = Microsoft.VisualBasic.Left(f.Name, (Len(f.Name) - 3))
strFileExt = Microsoft.VisualBasic.Right(strFileExt, 1)
If strFileExt = "h" Then
strFileExt = Microsoft.VisualBasic.Left(f.Name, (Len(f.Name) - 4))
strFileExt = Microsoft.VisualBasic.Right(strFileExt1, 1)
End If
If strFileExt = "." Then
IO.File.Copy(FileToCopy, NewPath & f.Name)
Else
IO.File.Copy(FileToCopy, NewPath & f.Name & f.Extension)
End If
'IO.File.Copy(FileToCopy, NewPath & f.Name & f.Extension )
Next
'Get all the SubFolders and Copy to next directory
Dim Path As String
For Each Path In strFolders
Dim di As New IO.DirectoryInfo(Path)
Dim SubFile As String = NewPath & di.Name & ""
IO.Directory.CreateDirectory(SubFile)
'Get all the files in the Sub folders
Dim FilePath As String
For Each FilePath In IO.Directory.GetFiles(di.FullName)
Dim fi As New IO.FileInfo(FilePath)
strFileExt1 = Microsoft.VisualBasic.Left(fi.Name, (Len(fi.Name) - 3))
strFileExt1 = Microsoft.VisualBasic.Right(strFileExt1, 1)
If strFileExt1 = "h" Then
strFileExt1 = Microsoft.VisualBasic.Left(fi.Name, (Len(fi.Name) - 4))
strFileExt1 = Microsoft.VisualBasic.Right(strFileExt1, 1)
End If
If strFileExt1 = "." Then
IO.File.Copy(FilePath, SubFile & fi.Name)
Else
IO.File.Copy(FilePath, SubFile & fi.Name & fi.Extension)
End If
'IO.File.Copy(FilePath, SubFile & fi.Name & fi.Extension )
Next
Next
End Sub
I hope this helps. The reason I did a check on the file extension was that the program was putting the file extension twice on the files.
-
Nov 2nd, 2005, 05:31 PM
#4
Re: I need a basic line of code to copy a folder/directory
 Originally Posted by Niche
FileCopy("C:\path1", "C:\path2")
Kill("C:\path1"
Try IO.File.Move instead for files. Here's how I would copy a folder:
VB Code:
Public Sub CopyFolder(ByVal source As String, _
ByVal target As String, _
ByVal overwrite As Boolean)
If Not IO.Directory.Exists(target) Then
'Create the target directory.
IO.Directory.CreateDirectory(target)
End If
'Copy files.
For Each file As String In IO.Directory.GetFiles(source)
IO.File.Copy(file, _
IO.Path.Combine(target, IO.Path.GetFileName(file)), _
overwrite)
Next
'Copy subfolders.
For Each subfolder As String In IO.Directory.GetDirectories(source)
Me.CopyFolder(subfolder, _
IO.Path.Combine(target, IO.Path.GetFileName(subfolder)), _
overwrite)
Next
End Sub
You may want to add some checking for existing files rather than just overwriting or not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|