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 (7)

  1. JOe VBslow says:

    SO why do you have the constants in there if they are never called?

       Const ForReading = 1

       Const ForWriting = 2

       Const ForAppending = 8

  2. Molo says:

    Joe VBslow: because he cut and paste it from somewhere else, made a few line cuts and never checked it?

  3. Josh says:

    Doesn't matter at least it works.  Thanks Bob!

  4. Rob says:

    I found a vbscript below that deletes txt files older than 7 days. Can someone wrap logging around this producing a logfile of script execution please? Would like to capture each deleted file name, data/time stamp of file and full path from where each deleted file is associated. Thx…….

    Modified script:

    '******************* Start of Code *************************

    Option Explicit

    On Error Resume Next

    Dim oFSO, oFolder, sDirectoryPath  

    Dim oFileCollection, oFile, sDir  

    Dim iDaysOld

    ' Specify Directory Path From Where You want to clear the old files

    sDirectoryPath = "f:test"

    ' Specify Number of Days Old File to Delete

    iDaysOld = 7

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    Set oFolder = oFSO.GetFolder(sDirectoryPath)

    Set oFileCollection = oFolder.Files

    For each oFile in oFileCollection

    'This section will filter the files to be deleted

    'Specify the Extension of file that you want to delete

    'and the number with Number of character in the file extension  

    If LCase(Right(Cstr(oFile.Name), 3)) = "txt" Then

    If oFile.DateLastModified < (Date() – iDaysOld) Then

      oFile.Delete(True)

    End If

    End If    

    Next

    Set oFSO = Nothing

    Set oFolder = Nothing

    Set oFileCollection = Nothing

    Set oFile = Nothing

    '************************ End of Code ********************

  5. Kevin says:

    "… because he cut and paste it from somewhere else, made a few line cuts and never checked it?"

    True.  But FYI: Those are for an additional argument in the .OpenTextFile method named iomode.  1 is read-only, 2 is overwrite, and 8 is append.  

  6. Webdav101 says:

    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.

  7. Karthiga says:

    Hi Guys ,

    Please let me know how to write the string(has numerous lines in the variable stored)into the file

Skip to main content