read file in VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • potluri040
    New Member
    • Dec 2006
    • 1

    #1

    read file in VB

    hi,
    could any one let me know how to read file thru VB script.

    Scenerio is like this:
    i am keeping a personal details such as name, age, sex, company, location in a text file called user details for some users and when I input the users name thru input box, a message box shd be appeared with the dtails from file.

    i would appreciate if i can get some code.
  • axtens
    New Member
    • Dec 2006
    • 32

    #2
    I use this function, or one like it.

    Code:
    Const AS_SYSTEMDEFAULT = -2
    
    Function ReadFile( FileName)
        Dim oStream
        Dim sData
    
        sData = vbNullString
    
        Set oStream = oFSO.OpenTextFile(FileName, CONST_READ, False, AS_SYSTEMDEFAULT )
        sData = oStream.ReadAll
        oStream.Close
    
        ReadFile = sData
    End Function
    This reads the entire file into memory. Then I split it into an array and work from there. For example:

    Code:
    Dim aFile
    aFile = Split( ReadFile( "somefile.txt" ), vbNewLine )
    WScript.Echo "First line", aFile( 0 )
    WScript.Echo "Last line", aFile( UBound( 0 ) )
    There are many more examples of file reading and writing routines on my weblog, Code-a-holic

    Comment

    Working...