' ****************************************************** ' * ' * Name: modReadTextFile.vbs ' * ' * Design Phase: ' * Author: John Miner ' * Date: 06/30/2008 ' * Purpose: A module to read text files. ' * ' ****************************************************** ' ' 2 - Define a module to read a text file ' ' Declare all variables Option Explicit ' Define constants Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 ' ' Define the class ' Class clsReadTxtFile ' File system object Dim objFso ' A file object Dim objFile ' End of file flag Dim EOF ' ' A - Define open method ' Public Sub OpenDataFile(strOpenFile, intMode) ' Set the flag to true If Not FileExists(strOpenFile) Then EOF = True Exit Sub Else EOF = False End if ' Create the object Set objFso = CreateObject("Scripting.FileSystemObject") ' Open the file Set objFile = objFso.OpenTextFile(strOpenFile, intMode, True) End Sub ' ' B - Define pull data method ' Public Function PullData() ' File does not exist? If EOF Then PullData = "" Exit Function End If ' End of file? If (objFile.AtEndOfStream) Then EOF = True PullData = "" Exit Function End If ' Read a line of data PullData = objFile.ReadLine End Function ' ' C - Define close method ' Public Sub CloseDataFile() ' If file does not exist On Error Resume Next ' Close the file objFile2.Close ' Release the file object Set objFile2 = Nothing ' Release the file system object Set objFso2 = Nothing End Sub ' ' D - Define file exists ' Public Function FileExists(strFile) ' Declare variables Dim objFSO ' Get the file system object Set objFSO = CreateObject("Scripting.FileSystemObject") ' Does the file exist? If objFSO.FileExists(strFile) Then FileExists = True Else FileExists = False End If ' Release the object Set objFSO = nothing End Function ' ' E - Define pull all data method ' Public Function PullAllData() ' File does not exist? If EOF Then PullAllData = "" Exit Function End If ' End of file? If (objFile.AtEndOfStream) Then EOF = True PullAllData = "" Exit Function End If ' Read a line of data PullAllData = objFile.ReadAll End Function End Class