Results 1 to 4 of 4

Thread: I need a basic line of code to copy a folder/directory

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    22

    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!

  2. #2
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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.

  3. #3
    Lively Member SonicBoomAu's Avatar
    Join Date
    Aug 2004
    Posts
    78

    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:
    1. System.IO.Directory.Move(sourcedir, targetdir)

    or this is what I use to copy all the files and folders

    VB Code:
    1. Imports System
    2. Imports System.IO
    3. Imports System.IO.IsolatedStorage
    4.  
    5. Private Sub Copy_Info()
    6.  
    7.         Dim strFolders() As String = IO.Directory.GetDirectories(Me.txtSourceLocation.Text)
    8.         Dim NewPath As String = strFullAccountTarget
    9.         Dim strFileExt As String
    10.         Dim strFileExt1 As String
    11.  
    12.  
    13.         'Get all the files in the main Directory
    14.         Dim MainFiles() As String = IO.Directory.GetFiles(Me.txtSourceLocation.Text)
    15.         Dim FileToCopy As String
    16.  
    17.         'For each file in Source Directory copy to Target Directory
    18.         For Each FileToCopy In MainFiles
    19.             Dim f As IO.FileInfo = New IO.FileInfo(FileToCopy)
    20.  
    21.             strFileExt = Microsoft.VisualBasic.Left(f.Name, (Len(f.Name) - 3))
    22.             strFileExt = Microsoft.VisualBasic.Right(strFileExt, 1)
    23.  
    24.  
    25.             If strFileExt = "h" Then
    26.                 strFileExt = Microsoft.VisualBasic.Left(f.Name, (Len(f.Name) - 4))
    27.                 strFileExt = Microsoft.VisualBasic.Right(strFileExt1, 1)
    28.             End If
    29.  
    30.             If strFileExt = "." Then
    31.                 IO.File.Copy(FileToCopy, NewPath & f.Name)
    32.             Else
    33.                 IO.File.Copy(FileToCopy, NewPath & f.Name & f.Extension)
    34.             End If
    35.  
    36.             'IO.File.Copy(FileToCopy, NewPath & f.Name & f.Extension )
    37.         Next
    38.  
    39.         'Get all the SubFolders and Copy to next directory
    40.         Dim Path As String
    41.         For Each Path In strFolders
    42.             Dim di As New IO.DirectoryInfo(Path)
    43.             Dim SubFile As String = NewPath & di.Name & ""
    44.             IO.Directory.CreateDirectory(SubFile)
    45.             'Get all the files in the Sub folders
    46.             Dim FilePath As String
    47.             For Each FilePath In IO.Directory.GetFiles(di.FullName)
    48.                 Dim fi As New IO.FileInfo(FilePath)
    49.  
    50.                 strFileExt1 = Microsoft.VisualBasic.Left(fi.Name, (Len(fi.Name) - 3))
    51.                 strFileExt1 = Microsoft.VisualBasic.Right(strFileExt1, 1)
    52.  
    53.                 If strFileExt1 = "h" Then
    54.                     strFileExt1 = Microsoft.VisualBasic.Left(fi.Name, (Len(fi.Name) - 4))
    55.                     strFileExt1 = Microsoft.VisualBasic.Right(strFileExt1, 1)
    56.                 End If
    57.  
    58.                 If strFileExt1 = "." Then
    59.                     IO.File.Copy(FilePath, SubFile & fi.Name)
    60.                 Else
    61.                     IO.File.Copy(FilePath, SubFile & fi.Name & fi.Extension)
    62.                 End If
    63.  
    64.                 'IO.File.Copy(FilePath, SubFile & fi.Name & fi.Extension )
    65.             Next
    66.         Next
    67.  
    68. 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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I need a basic line of code to copy a folder/directory

    Quote 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:
    1. Public Sub CopyFolder(ByVal source As String, _
    2.                           ByVal target As String, _
    3.                           ByVal overwrite As Boolean)
    4.         If Not IO.Directory.Exists(target) Then
    5.             'Create the target directory.
    6.             IO.Directory.CreateDirectory(target)
    7.         End If
    8.  
    9.         'Copy files.
    10.         For Each file As String In IO.Directory.GetFiles(source)
    11.             IO.File.Copy(file, _
    12.                          IO.Path.Combine(target, IO.Path.GetFileName(file)), _
    13.                          overwrite)
    14.         Next
    15.  
    16.         'Copy subfolders.
    17.         For Each subfolder As String In IO.Directory.GetDirectories(source)
    18.             Me.CopyFolder(subfolder, _
    19.                           IO.Path.Combine(target, IO.Path.GetFileName(subfolder)), _
    20.                           overwrite)
    21.         Next
    22.     End Sub
    You may want to add some checking for existing files rather than just overwriting or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width