[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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.


0
99Times
Asked:
99Times
2 Solutions
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now