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.


LVL 3
99TimesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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
    Wend
    Close #ff
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mvidasCommented:
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
 Loop
 Close #vFF
End Sub


Matt
0
rettiseertCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.