Link to home
Start Free TrialLog in
Avatar of 99Times
99Times

asked on

Read a Text File and copy the files within

I have a text file that has file paths in it

Image.txt

eg.
-----
c:\folder\image1.jpg
c:\folder\image2.jpg
c:\folder\image3.jpg
c:\folder\image4.jpg


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.

Thanks.


ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rettiseert
rettiseert

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
    Loop
   
    Close #FF
   

End Function

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