Microsoft Scripting Runtime #4

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    Microsoft Scripting Runtime #4

    This is the last in a series of Tips involving the Microsoft Scripting Runtime Library and deals with creating, opening, writing to, reading from, and closing Text Files via this Library. At this time, the Scripting Library cannot deal with the opening and manipulation of Files in Binary Mode, so we will only demonstrate this functionality as it relates to Text Files. The code is fairly straightforward and sparsely commented, so I will not bore you with unnecessary details. The code is posted below, any questions, please feel free to ask them:
    [CODE=vb]
    '************** *************** *************** *************** *******
    Dim fso As FileSystemObjec t, ts As TextStream, fil As File, txtFile
    'Set fso = CreateObject(“S cripting.FileSy stemObject”) OR
    Set fso = New FileSystemObjec t
    '************** *************** *************** *************** *******

    'Opening a Text File and Reading its contents
    Set ts = fso.OpenTextFil e("C:\Test\Test .TXT", ForReading, False, TristateUseDefa ult)

    Debug.Print ts.ReadAll 'Reads entire File contents
    Debug.Print ts.Read(500) 'Reads 500 characters
    Debug.Print ts.ReadLine 'Reads 1 entire line
    Do Until ts.AtEndOfStrea m 'Reads entire File line by line
    Debug.Print ts.ReadLine
    Loop

    ts.Close
    Set ts = Nothing

    '************** *************** *************** *************** *******

    '3 Methods to create a Sequential Text File
    Set ts = fso.CreateTextF ile("C:\Test\Te xtFile1.txt", True)

    Set ts = fso.OpenTextFil e("C:\Test\Test .txt", ForWriting)

    fso.CreateTextF ile ("C:\Test\TestF ile3.txt")
    Set fil = fso.GetFile("C: \Test\TestFile3 .txt")
    Set ts = fil.OpenAsTextS tream(ForWritin g)

    '************** *************** *************** *************** *******

    'How to Open a File, use the 3 Write Methods to add data to
    'the File, then Close the File

    Set txtFile = fso.CreateTextF ile("C:\Test\Te xtFile.txt", True)
    txtFile.Write ("This is just a test. ") 'Write a Line

    'Write a Line with a NewLine character
    txtFile.WriteLi ne ("Testing 1, 2, 3.")
    txtFile.Write "Test for the NewLine character!"

    'Write 3 NewLine characters to the File
    txtFile.WriteBl ankLines (3)
    txtFile.Write "Just to see if we have 3 blank lines!"
    txtFile.Close

    Set txtFile = fso.CreateTextF ile("C:\Test\Te xtFile.txt", True)
    txtFile.Close 'Critical

    'Write a Line
    Set fil = fso.GetFile("C: \Test\Textfile. txt")
    Set ts = fil.OpenAsTextS tream(ForWritin g) 'ForAppending, ForReading
    ts.Write "Hello World"
    ts.Close

    '************** *************** *************** *************** *******

    'Opening a File for Reading
    Set fil = fso.GetFile("C: \Test\Textfile. txt")
    Set ts = fil.OpenAsTextS tream(ForReadin g)

    'Read the contents of the File
    S = ts.ReadLine 'Reads 1st line into S
    S = ts.Read(25) 'Reads 25 characters into S
    S = ts.ReadAll 'Read entire file into S
    MsgBox S
    ts.Close
    [/code]
Working...