Results 1 to 5 of 5

Thread: Loading Text File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    64
    What code can I use to load all the text from a text file into a variable?

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    try this...this is for one field in , delimited text file

    ' Open file for input.
    Open "TESTFILE" For Input As #1

    ' Loop until end of file.
    Do While Not EOF(1)

    ' Read data into two variables.
    Input #1, MyString

    ' Print data to Debug window.
    Debug.Print MyString

    Loop
    ' Close file.
    Close #1
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    If you want to load the entire text file into one single string, you might want this code:

    Code:
    Option Explicit
    
    Function LoadFile(Filename As String) As String
    
      '*** Dimension local variables...
      Dim fileNum  As Integer               ' file number
      Dim fileLen  As Long                  ' file length
      Dim fileData As String                ' file data
    
      fileNum = FreeFile                    ' get file handle
      Open Filename For Binary As #fileNum  ' open file
      fileLen = LOF(fileNum)                ' get file length
      fileData = Space$(fileLen)            ' set buffer size
      Get #fileNum, , fileData              ' read file data
      Close #fileNum                        ' close file
      
      LoadFile = fileData                   ' return value
    
    End Function
    Use the LoadFile function like this to load your AUTOEXEC.BAT for example:

    Code:
    Dim myFile As String
    
    myFile = LoadFile("c:\autoexec.bat")
    Notice that this function will load info from ANY file into the string variable, i.e. executable files, graphics files, text files, wave files, you name it.

    Hope this helps.

  4. #4
    New Member
    Join Date
    May 2000
    Posts
    14
    Two methods I've used in the past...

    Code:
    Dim inFile&, lne$, text$
    inFile = FreeFile
    Open "c:\file.dat" for Input as inFile
    Line Input #inFile, text
    While Not EOF(inFile)
        Line Input #inFile, lne
        text = text + vbCrLf + lne
    Wend
    Close inFile
    Code:
    Dim inFile&, text$
    inFile = FreeFile
    Open "c:\file.dat" for Input as inFile
    text = Input(LOF(inFile), #inFile)
    Close inFile


    Shab.

    Code:
    Print WeekDayName(vbMonday)

  5. #5
    New Member
    Join Date
    Jun 2000
    Location
    Singapore
    Posts
    8

    Thumbs up

    This one's even faster!
    Code Snippet below:
    -------------------

    Option Explicit

    Private Sub Command1_Click()

    'Load Text from file (Filename: MyFile.txt)
    Open App.Path & "\MyFile.txt" for input as #1
    Text1.Text = Input(LOF(1), #1)
    Close

    End Sub
    -------------------

    A line of code does the trick.

    You can do a search on the word 'input' in MSDN and click on 'Input Function'. The code syntax will be shown. Hope this helps!
    PiKaPrO
    =======
    PiKa ProGraMMeR © 2000
    [email protected] No SPAMMERS!
    MSVB6.0 PRO SP3 Detected.

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