Results 1 to 9 of 9

Thread: Looping through text file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954

    Looping through text file

    I have a text file that I would like to loop through and obtain a listing of vendors. It is a file that is seperated by the "^" simble. I would like to loop through it and place the results in a combo box. Here is the code I have so far:

    Function getVendors(offnum)
    Const ForReading = 1
    Const Create = False

    ' Declare local variables
    strFileName = Server.MapPath("vendors.txt")

    ' Instantiate the FileSystemObject
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

    ' use Opentextfile Method to Open the text File
    Set TS = objFSO.OpenTextFile(strFileName, ForReading, Create)

    strValue = Ts.Readline
    strArray = Split(strValue, "^")
    ' - strArray is now an array
    Dim i
    If Not TS.AtEndOfStream Then
    For i = 0 to Ubound(strArray)
    'Response.Write i & " = " & strArray(i) & "<BR>"

    VendorName = strArray(0)
    VendorAddr = strArray(1)
    VendorCity = strArray(2)
    VendorState = strArray(3)
    VendorZip = strArray(4)
    VendorID = strArray(5)
    VendorOff = strArray(6)
    Next
    End IF
    ' close TextStreamObject
    ' and destroy local variables to relase memory
    'TS.Close
    'Set TS = Nothing
    'Set objFSO = Nothing
    End Function

    This is how I am calling it:

    <option value="002"><%If Not TS.AtEndOfStream Then %><%=VendorName%>,<%=VendorAddr%>&nbsp<%=VendorCity%>,<%=vendorzip%><%END IF%></option>

    The problem is that is only goes through the first line, then it stops. It places the first line in the combo box but that is it. How can I get all of them in there, also can you sort with a text file?

    Attached is the text file...Thanks
    Attached Files Attached Files

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Here is an example of looping through a text file using FSO

    VB Code:
    1. dim val
    2.  
    3.   do while f.AtEndOfStream = false
    4.     val=f.ReadLine
    5.     Response.Write "<option value='" & val & "'>" & val & "</option>"
    6.   loop
    7.  
    8.   f.close
    9.   Set f = Nothing
    10.   Set fso = Nothing

    Hope this helps.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954
    Great, do you know how to sort using the file system object?

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by brianh
    Great, do you know how to sort using the file system object?
    If you search on this forum you will get plenty of example of sorting algorithm, look for bubble sort. Here is an example using fso, u can adapt it to your need.

    http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=118

    Hope this helps.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Ack nooooooooooooooooooo.
    Never ever ever use the FSO until absolutely necessary because its crap
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Sorry, only now just noticed where this thread was.
    Um I don't know if there's any other ways of doing it in ASP etc, so just ignore me
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by plenderj
    Sorry, only now just noticed where this thread was.
    Um I don't know if there's any other ways of doing it in ASP etc, so just ignore me
    Yes Jamie, I dont like to use FSO either, but in asp its the easiest and quickest way. There is other way of doing it, but more time consuming.

    I have never really used FSO in VB but in asp i use it frequently.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  8. #8
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    In ASP, load the file contents line by line into a Scripting.Dictionary object. This doesn't need to be sorted (provided you index it correctly), as it's searchable!
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  9. #9
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by Jerry Grant
    In ASP, load the file contents line by line into a Scripting.Dictionary object. This doesn't need to be sorted (provided you index it correctly), as it's searchable!
    Well Dictionary object is pretty similar or if not the same to VB Collection Object. You can use it to store information temporerily but not permanently. I have to admint it is pretty useful sometime.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width