Connecting Tech Pros Worldwide Forums | Help | Site Map

Microsoft Scripting Runtime #4

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#1   Feb 23 '08
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:
Expand|Select|Wrap|Line Numbers
  1. '******************************************************************
  2. Dim fso As FileSystemObject, ts As TextStream, fil As File, txtFile
  3. 'Set fso = CreateObject(“Scripting.FileSystemObject”)       OR
  4. Set fso = New FileSystemObject
  5. '******************************************************************
  6.  
  7. 'Opening a Text File and Reading its contents
  8. Set ts = fso.OpenTextFile("C:\Test\Test.TXT", ForReading, False, TristateUseDefault)
  9.  
  10. Debug.Print ts.ReadAll             'Reads entire File contents
  11. Debug.Print ts.Read(500)           'Reads 500 characters
  12. Debug.Print ts.ReadLine            'Reads 1 entire line
  13. Do Until ts.AtEndOfStream          'Reads entire File line by line
  14.   Debug.Print ts.ReadLine
  15. Loop
  16.  
  17. ts.Close
  18. Set ts = Nothing
  19.  
  20. '******************************************************************
  21.  
  22. '3 Methods to create a Sequential Text File
  23. Set ts = fso.CreateTextFile("C:\Test\TextFile1.txt", True)
  24.  
  25. Set ts = fso.OpenTextFile("C:\Test\Test.txt", ForWriting)
  26.  
  27. fso.CreateTextFile ("C:\Test\TestFile3.txt")
  28. Set fil = fso.GetFile("C:\Test\TestFile3.txt")
  29. Set ts = fil.OpenAsTextStream(ForWriting)
  30.  
  31. '******************************************************************
  32.  
  33. 'How to Open a File, use the 3 Write Methods to add data to
  34. 'the File, then Close the File
  35.  
  36. Set txtFile = fso.CreateTextFile("C:\Test\TextFile.txt", True)
  37. txtFile.Write ("This is just a test. ")     'Write a Line
  38.  
  39. 'Write a Line with a NewLine character
  40. txtFile.WriteLine ("Testing 1, 2, 3.")
  41. txtFile.Write "Test for the NewLine character!"
  42.  
  43. 'Write 3 NewLine characters to the File
  44. txtFile.WriteBlankLines (3)
  45. txtFile.Write "Just to see if we have 3 blank lines!"
  46. txtFile.Close
  47.  
  48. Set txtFile = fso.CreateTextFile("C:\Test\TextFile.txt", True)
  49. txtFile.Close       'Critical
  50.  
  51. 'Write a Line
  52. Set fil = fso.GetFile("C:\Test\Textfile.txt")
  53. Set ts = fil.OpenAsTextStream(ForWriting)   'ForAppending, ForReading
  54. ts.Write "Hello World"
  55. ts.Close
  56.  
  57. '******************************************************************
  58.  
  59. 'Opening a File for Reading
  60. Set fil = fso.GetFile("C:\Test\Textfile.txt")
  61. Set ts = fil.OpenAsTextStream(ForReading)
  62.  
  63. 'Read the contents of the File
  64. S = ts.ReadLine     'Reads 1st line into S
  65. S = ts.Read(25)     'Reads 25 characters into S
  66. S = ts.ReadAll      'Read entire file into S
  67.   MsgBox S
  68.   ts.Close
  69.  



Reply