Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Text from File

Status
Not open for further replies.

jaylaban

Technical User
Feb 6, 2002
2
US
I am looking for a sample script. I am trying to read a text file line by line (parse) and spit each line into a different variable using vbs. Can anyone Help.

Thanks in Advance,
Jayson
 
Code:
Set fso = Application.CreateObject("Scripting.FileSystemObject")
strFile = "C:\Path\To\File\File.txt"

    If fso.FileExists(strFile) Then
         Set FilePath = fso.GetFile(strFile)
         Set OpenFile = FilePath.OpenAsTextStream(1)
             Do While Not OpenFile.AtEndOfStream
                 tmpStr = OpenFile.ReadLine
                 varArray = Split(tmpStr, "'--insert delimiter--'")
                 '--Do stuff with array here--'
             Loop
         OpenFile.Close
     End If
Hope that helped! :)

Oh wait...I think I misread that. Are you trying to put each individual Line into a separate value? Or break up each line...?
 
This looks great! I am trying to put each individual line into a seperate value.
 
Well, I'm sure someone will slap me if I'm wrong, but the code above is accomplishing what you'd like it to. Every time the Do Loop runs it pulls one line from the file and assigns it to the tmpStr variable.

While within the Do Loop, you can manipulate and test that variable all you want:
Code:
Do While Not OpenFile.AtEndOfStream
    tmpStr = OpenFile.ReadLine

' If you're testing each line as a complete
' string, you can omit the varArray line.

        If tmpStr(Test1) Then
        --do something--
        ElseIf tmpStr(Test2) Then
        --do something else--
        ElseIf tmpStr(Test3) Then
        --etc.--
        End If
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top