Read a Text File and copy the files within

Posted on 2006-04-10
Last Modified: 2010-04-07
I have a text file that has file paths in it



I want to copy all of these files to a different directory (c:\folder\newFolder\) while presserving the file name.

I know the syntax for copying a file to different directory, but I don't know how to read a text file and loop over it.


Question by:99Times
    LVL 85

    Accepted Solution

    Private Sub Command1_Click()
        Dim ff As Integer
        Dim fileName As String
        Dim inputLine As String
        ff = FreeFile
        fileName = "c:\someFile.txt"
        Open fileName For Input As #ff
        While Not EOF(ff)
            Line Input #ff, inputLine
            Debug.Print inputLine
        Close #ff
    End Sub
    LVL 35

    Assisted Solution

    Hi 99,

    Using roughly the same method as Idle_Mind showed to open the file, this should do exactly as you need:

    Sub NinetyNineTimesCopyFiles()
     Dim vFF As Long, vFile As String, vDestPath As String, tempStr As String
     vFile = "C:\image.txt" 'source file with image listing
     vDestPath = "C:\folder name\" 'directory to copy images to
     If Right(vDestPath, 1) <> "\" Then vDestPath = vDestPath & "\"
     vFF = FreeFile
     Open vFile For Input As #vFF
     Do Until EOF(vFF)
      Line Input #vFF, tempStr
      If Len(Dir(tempStr)) > 0 Then 'if file exists...
       FileCopy tempStr, vDestPath & Dir(tempStr) 'copy file to destination directory
      End If
     Close #vFF
    End Sub

    LVL 13

    Expert Comment

    Private Sub Command1_Click()

        CopyFilesInTXT "c:\source.txt", "c:\destination\"    '<--- Will copy all files in source.txt to c:\destination\ (don forget to put a '\' at the end!)

    End Sub

    Private Function CopyFilesInTXT(strTXT As String, strDestinationFolder As String)

        Dim FF As Long
        Dim strLine As String
        FF = FreeFile
        Open strTXT For Input As FF
        Do While Not EOF(1)
            Line Input #1, strLine
            If Dir(strLine) <> "" Then
                 FileCopy strLine, strDestinationFolder + GetFileName(strLine)
            End If
        Close #FF

    End Function

    Private Function GetFileName(strFullPath As String) As String
        Dim Start As Integer
        Dim LastStart As Integer
            LastStart = Start
            Start = InStr(LastStart + 1, strFullPath, "\")
        Loop While Start <> 0
        GetFileName = Mid$(strFullPath, LastStart + 1)
    End Function


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now