Option Explicit '. Const ForWriting = 8 '.
VBScript » TextStream » ReadAll. Version: 2.0. Syntax: object.ReadAll. This method reads the entire contents of a text file and returns it as a string. This method reads the entire contents of a text file and returns it as a string.
Dim strGFI, objFSO1, objOutputFile Dim strOTF, MyString, objFileSystem Dim strWSE, strWritePath, objFile Dim strInput, cFOL, StrDirectory '. cFOL = InputBox ( 'Enter the path to search' & VbCrLf & '(e.g., C:temp)' ) strInput = Inputbox ( 'Enter the text you would like to search for.' ) MyString = 'FileSearch' strWritePath = 'c:scripts' & MyString & '.txt' strDirectory = 'C:scripts' Set objFSO1 = CreateObject ( 'Scripting.FileSystemObject' ) If objFSO1.
FileExists ( strWritePath ) Then 'Wscript.Echo 'The file Exists' Else Set objFile = objFSO1. CreateTextFile ( strDirectory & MyString & '.txt' ) objFile = ' End If Set objFileSystem = CreateObject ( 'Scripting.fileSystemObject' ) Set objOutputFile = objFileSystem. OpenTextFile ( strDirectory & MyString & '.txt', ForWriting ) If cFOL ' Then If strInput ' Then Dim objFSO Set objFSO = CreateObject ( 'Scripting.FileSystemObject' ) Dim objGFO Set objGFO = objFSO. GetFolder ( cFOL ) Dim objGFI Set objGFI = objGFO. Files For Each strGFI in objGFI Dim objOTF Set objOTF = objFSO.
OpenTextFile ( cFOL & ' & strGFI. Name, 1 ) Do While Not objOTF. AtEndOfStream strOTF = objOTF. ReadAll Loop objOTF. Close Set objOTF = Nothing '. If InStr ( LCase ( strOTF ), strInput ) 0 Then strWSE = strWSE & strGFI. Name & ' contains ' & strInput & vbCrLf Else strWSE = strWSE & strGFI.
Name & ' Does not contain: ' & strInput & vbCrLf End If Next objOutputFile. WriteLine ( strWSE ) objOutputFile. Close '. Set objGFI = Nothing Set objGFO = Nothing Set objFSO = Nothing Set objFileSystem = Nothing Set objFSO1 = Nothing '.
WScript. Echo strWSE Else Wscript. Echo 'You Clicked Cancel or no search string was defined.' End If Else Wscript. Echo 'You Clicked Cancel or no search path was defined.' Thank you for the respond.
The script above search for any string or number I would put into inputbox. Let's say I put in 38989, the script shows me 3 files and one of the files has 38988 which I do not want to see.
Is there a way I can tell the script to only search the character or number I input. If I input xxx123, it should not show me xxx124. This is the reason I tried to read from the last 2 lines of the file, because the correct number is on those lines. Thank you again. I hope I explained it clearly.:). Thank you for the script, but I am having a little trouble.I am trying to search through files to see if a specific string value is there.
But it doesn't look like it only searches for the specific string but any text containing that string. So for instance, I am looking for spdsys but it returns that multiple files contain this string. This string is at the beginning of lines of text like spdSysMax, I would only like to find spdsys and not the other texts that contain spdsys like spdsysmax. Any thoughts?.
Using the FileSystemObject to Read Text FilesIn the, we discussed that the general purpose of the FileSystemObject (FSO)is to allow access to the host computer's file system. One common function when dealing with a computer'sfile system, is reading the contents of a file. FSO provides a TextStream object that isuseful for reading and writing the contents of a text file. In this FAQ, we will discuss how to usethe TextStream object to read the contents of a file.When using the TextStream object or other FSO objects, you need to first declare an instanceof the FileSystemObject. For our script, let's start by creating an instance of theFileSystemObject:'Create an instance of the FileSystemObject objectDim objFSOSet objFSO = Server.CreateObject('Scripting.FileSystemObject')Before we open a file, we should always test to make sure it exists. The FileSystemObjectprovides a useful function to determine whether or not a file exists, aptly named FileExists.The syntax for FileExists is:FileSystemObjectInstance.FileExists( FullFilePath)So, in this example, we want to output the contents of the text file D:scottresume.txt.Before we open our file, we will make sure that it exists.Earlier I mentioned that the TextStream object is responsible for reading and writing tothe contents of a text file. A TextStream object instance is used to iterate through thecontents of a particular text file.
So, before we can use a TextStream, we must inform theTextStream object instance what text file we are interested in. This is accomplished usingthe FileSystemObject's OpenTextFile method. The OpenTextFile methodhas the following syntax:FileSystemObjectInstance.OpenTextFile( FullFilePath, iomode)Note: there are two more parameters, but we are only interested in these first two for the timebeing. If you are interested in the full specifications,.The optional iomode parameter decides what IO mode the file is opened with. The followingvalues can be used:iomode ValuesForReading1ForWriting2ForAppending8These constants are not defined in a text file like the ADO constants are defined in. Therefore, you should definethe constant you wish to use.
For example, if I use the ForReading constant, youwill see ASP code like:Const fsoForReading = 1The OpenTextFile method returns a TextStream object instance. So, to set avariable to a TextStream object instance for reading the contents of a particular file, youwould use the following syntax:'Assumes a FileSystemObject instance named objFSO existsConst fsoForReading = 1Dim objTextStreamSet objTextStream = objFSO.OpenTextFile('C:SomeFile.txt', fsoForReading)The TextStream object has the following important methods for reading text files:. Read( characters) - Reads a specified number of characters from thetext file, returning a resulting string. ReadLine - Reads an entire line up to, but not including, the new line character. Theresults are returned as a string. ReadAll - Reads the entire contents of the file, returning the results as a string.Finally, the TextStream object has a Close method, which should be used whenyou have finished reading from the file.Now, let's look at how we could read the entire contents of a text file, D:scottresume.txt.We will use the FileExists method first, to make sure that the file does indeed exist. If itdoes, we'll output the contents of the file; if it doesn't, we'll display an appropriate message.Dim objFSOSet objFSO = Server.CreateObject('Scripting.FileSystemObject')Dim objTextStreamconst strFileName = 'D:scottresume.txt'const fsoForReading = 1If objFSO.FileExists('D:scottresume.txt') then'The file exists, so open it and output its contentsSet objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)Response.Write ' ' & objTextStream.ReadAll & 'objTextStream.CloseSet objTextStream = NothingElse'The file did not existResponse.Write strFileName & ' was not found.'
End If'Clean upSet objFSO = NothingWell, there you have it, reading the contents of a text file using the FileSystemObject andTextStream objects. A closing note: whenever you use objects, you shouldalways do Set Object = Nothing when you are done using them.In our, we will discuss how to write to text files using the FileSystemObject andTextStream objects.Happy Programming!.