HOWTO: Write a file using VBScript

 '-------------------------------------------------------------------------------
' WriteFileText - Used to write an item to a file in a folder.
' Parameters:
'   sFile - The file to read
'
' Returns:
'   A string containing the content of the file.
'-------------------------------------------------------------------------------
Function WriteFileText(sFilePath, sText)
    Dim objFSO 'As FileSystemObject
    Dim objTextFile 'As Object
   
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.CreateTextFile(sFilePath, True)
   
    ' Write a line.
    objTextFile.Write (sText)

    objTextFile.Close
    'objTextFile.Close

End Function

Comments

  • Anonymous
    May 22, 2012
    SO why do you have the constants in there if they are never called?    Const ForReading = 1    Const ForWriting = 2    Const ForAppending = 8

  • Anonymous
    June 25, 2012
    The comment has been removed

  • Anonymous
    November 22, 2012
    Doesn't matter at least it works.  Thanks Bob!

  • Anonymous
    October 22, 2013
    The comment has been removed

  • Anonymous
    February 21, 2014
    The comment has been removed

  • Anonymous
    February 21, 2014
    It was just a quick utility code snip I added.  Just ignore those two lines.  As the title notes the snip is about writing to a file.  I don't usually post such simple scripts on working with files.  This post is just to provide a basic sample for writing to a file which can be used for samples tied to the primary topics I blog on - Exchange and Outlook related APIs.  I took it from a larger script I wrote a very, very long time ago.

  • Anonymous
    June 01, 2015
    Hi Guys , Please let me know how to write the string(has numerous lines in the variable stored)into the file