Results 1 to 6 of 6

Thread: Copy multiple files

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Copy multiple files

    VB Code:
    1. FileCopy(strSource, strDestination)

    This is the way i know to copy one file from one directory to other. But how about copying multiple files? Like copying *.txt from Folder1 to Folder2.

    pls guide

    regards
    Microsoft Techie

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this , I didn't test it though .
    VB Code:
    1. Try
    2.             Dim files() As String = Directory.GetFiles("c:\folder", "*.txt")
    3.             For Each fil As String In files
    4.                 File.Copy(fil, destnationFolder)
    5.             Next
    6.         Catch ex As Exception
    7.             MessageBox.Show(ex.Message)
    8.         End Try

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This one tested and working :

    VB Code:
    1. Private Sub CopyFiles()
    2.         Const desntationfolder As String = "c:\folder2"
    3.  
    4.         Try
    5.             Dim files() As String = Directory.GetFiles("c:\folder1", "*.txt")
    6.             For Each fil As String In files
    7.                 File.Copy(fil, desntationfolder + "\" + Path.GetFileName(fil))
    8.             Next
    9.         Catch ex As Exception
    10.             MessageBox.Show(ex.Message)
    11.         End Try
    12.  
    13.     End Sub

  4. #4
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120
    isn't there any option rather than looping around the files??

    just an alternative

    regards
    with Regards
    sameer Mulgaonkar

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by sameer spitfire
    isn't there any option rather than looping around the files??

    just an alternative
    regards
    And what's wrong with loop structures ? There might be other alternatives but can't think of any now .

  6. #6
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: Copy multiple files

    Hello guys, I have 2 folders. A source and a destination folder.
    Destination folder is a folder in my "Program files", and I want to make an application to look for new files from source folder, and copy them to destination folder.
    In source folder, it is possible to find only files, or files and folders and subfolders.
    My problem is that I want to copy only files, not folders.
    I want my application to navigate to each subfolder copy files to the same subfolder of destination folder in "Program files"
    My problem is that I don't know how to take the destination
    folder & subfolder & file.name in a string.

    Thanks for any help

    Code:
        Private Sub CopyFilesDir(ByVal sDir As DirectoryInfo, ByVal dDir As DirectoryInfo)
            Dim dirs() As DirectoryInfo = sDir.GetDirectories
            Dim dir As DirectoryInfo
            Dim sf1 As String
    
            For Each dir In dirs
    
                For Each f As FileInfo In dir.GetFiles
                    sf1 = dir.FullName & "\" & f.Name
                    Copyfiles(sf1, destination, true)
                Next
    
            Next
        End Sub

    EDIT: I did it with split string.

    Code:
    Private Sub SplitPath(ByVal s As String)
            LineOfText = s
            aryTextFile = LineOfText.Split("\")
            Dim i As Integer
            For i = 0 To UBound(aryTextFile)
                If i >= 5 Then
                    sPath &= aryTextFile(i) & "\"
                Else
                    sPath = ""
                End If
            Next i
    Last edited by manin; Nov 13th, 2009 at 05:30 PM.

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