Results 1 to 6 of 6

Thread: importing file contents into a variable

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    importing file contents into a variable

    Hi all.
    I've been using this code for years to get a file's contents into a variable:

    Open strFile For Input As #1
    strX = ""
    Do Until EOF(1) = True
    Line Input #1, x
    strX = strX & x
    Loop
    Close #1

    Is there a faster way?

    I'm moving a lot of files in a loop, so every second counts.
    Thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: importing file contents into a variable

    I guess I forgot about this:

    strX = Input(LOF(1), #1)


    anything faster than this?
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: importing file contents into a variable

    also,
    for downloading html files,
    anything faster than this:

    Public Declare Function URLDownloadToFile Lib "urlmon" Alias _
    "URLDownloadToFileA" (ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) As Long

    Public Function DownloadFile(URL, LocalFilename) As Boolean
    Dim lngRetVal As Long
    lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    If lngRetVal = 0 Then DownloadFile = True
    End Function
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: importing file contents into a variable

    To my knowledge, this is the fastest possible way:
    Code:
    Dim MyData As String
    Open "MyTextFile.Txt" For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    MyData now contains all of the file as a string variable.
    Doctor Ed

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: importing file contents into a variable

    Quote Originally Posted by wengang View Post
    I'm moving a lot of files in a loop, so every second counts.
    Moving files? As in reading a file, writing the data (unchanged) to a new file, then deleting the old file? If so, try the Name statement:

    Name oldpathname As newpathname

  6. #6
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: importing file contents into a variable

    this link may be useful
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


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