Howto: VBScript - Read a file

'-----------------------------------------------------------------------------------
' ReadFileText - Used to read  a file in a folder.
' Parameters:
'   sFile - The file to read
'
' Returns:
'   A string containing the content of the file.
'-----------------------------------------------------------------------------------
Function ReadFileText (sFile)
    Dim objFSO 'As FileSystemObject
    dim oTS
    dim sText
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set oTS = objFSO.OpenTextFile(sFile)
    sText = oTS.ReadAll

    oTS.close
    set oTS = nothing
    Set objFSO = nothing
 
    ReadFileText = sText
    
end Function

Comments

  • Anonymous
    June 25, 2011
    how can file content display in a web page

  • Anonymous
    August 18, 2011
    Lowsy code snippet.  There are no comments that tell what anything does.  For example, what specifically does the ReadAll function do, and are there other functions that also read from text files (Read, etc.).  Without any explanation, the code sample doesn't teach anything.

  • Anonymous
    August 18, 2011
    As very basic FSO sample code it is not meant to teach now to use the API in detail - it just shows an example of implementation.  This sample was provided since its processing handles the basic need to read an entire file - such functionality is commonly needed for testing.  This blog is more geared to APIs related to working wiht Exchange and Outlook rather than with ohter APIs such as FSO. For specifics, check MSDN (msdn.microsoft.com/.../czxefwt8(v=VS.85).aspx) or search on "Scripting.FileSystemObject".

  • Anonymous
    March 25, 2013
    Thanks, the snippet has all is required, the code is pretty much self explanatory (to those with basic understanding...)