Find specific string in a text file with VB script

Find specific string in a text file with VB script

To find a specific string in a text file using a VBScript (VBS), you can read the file line by line and search for the desired string. Here's a simple example of how you can achieve this:

Dim strFile, strSearchString Dim objFSO, objFile, objTextStream Dim strLine, lineNumber ' File path and search string strFile = "C:\path\to\your\file.txt" strSearchString = "specific string" ' Create a File System Object Set objFSO = CreateObject("Scripting.FileSystemObject") ' Check if the file exists If objFSO.FileExists(strFile) Then ' Open the file for reading Set objFile = objFSO.GetFile(strFile) Set objTextStream = objFile.OpenAsTextStream(ForReading) ' Initialize line number lineNumber = 1 ' Read through the file line by line Do While Not objTextStream.AtEndOfStream ' Read each line strLine = objTextStream.ReadLine() ' Check if the line contains the search string If InStr(1, strLine, strSearchString, vbTextCompare) > 0 Then ' Print the line number and the line itself WScript.Echo "Line " & lineNumber & ": " & strLine End If ' Increment line number lineNumber = lineNumber + 1 Loop ' Close the text stream and file objects objTextStream.Close Set objTextStream = Nothing Set objFile = Nothing Else ' Handle case where file does not exist WScript.Echo "File not found: " & strFile End If ' Clean up Set objFSO = Nothing 

Explanation:

  1. Variables:

    • strFile: Specifies the path to the text file you want to search.
    • strSearchString: Specifies the string you want to search for within the text file.
  2. File System Object (objFSO):

    • Creates an instance of the Scripting.FileSystemObject to work with files and folders.
  3. Opening and Reading the File:

    • Checks if the file exists using FileExists.
    • If the file exists, opens the file using GetFile and OpenAsTextStream for reading.
  4. Searching for the String:

    • Reads each line in the file using ReadLine.
    • Checks if the line contains the strSearchString using InStr.
    • InStr function returns the position of the first occurrence of strSearchString in strLine or 0 if not found.
  5. Output:

    • If the string is found in a line, prints the line number and the line itself using WScript.Echo.
  6. Cleanup:

    • Closes the objTextStream and releases resources.

Notes:

  • Adjust strFile and strSearchString to match your actual file path and the string you want to search for.
  • This script is case-insensitive (vbTextCompare), meaning it will find both "Specific String" and "specific string".
  • Ensure that you have appropriate permissions to read the file specified by strFile.

Run this VBScript on Windows using the cscript or wscript command-line tools or by double-clicking the .vbs file. It will search through the specified text file and output any lines containing the specified search string. Adjust the script as needed for more complex search patterns or additional file handling requirements.

Examples

  1. VBScript find string in text file

    • Description: Search for a specific string in a text file using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strLine strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine, strSearchString) > 0 Then WScript.Echo "Found: " & strSearchString & " in file." Exit Do End If Loop objFile.Close 
  2. VBScript find and count occurrences in text file

    • Description: Find and count occurrences of a specific string in a text file using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strLine, count strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_string_to_search" count = 0 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine count = count + (Len(strLine) - Len(Replace(strLine, strSearchString, ""))) / Len(strSearchString) Loop objFile.Close WScript.Echo "Occurrences found: " & count 
  3. VBScript search text file and return line

    • Description: Search a text file for a specific string and return the matching line using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strLine strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine, strSearchString) > 0 Then WScript.Echo "Found in line: " & strLine Exit Do End If Loop objFile.Close 
  4. VBScript find string in multiple text files

    • Description: Search for a specific string in multiple text files using VBScript.
    • Code:
      Const ForReading = 1 Dim strFolder, strSearchString, objFSO, objFile, strLine strFolder = "C:\Path\To\Your\Folder\" strSearchString = "your_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") For Each objFile In objFSO.GetFolder(strFolder).Files If objFSO.GetExtensionName(objFile.Path) = "txt" Then Set objFile = objFSO.OpenTextFile(objFile.Path, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine, strSearchString) > 0 Then WScript.Echo "Found in file: " & objFile.Path Exit Do End If Loop objFile.Close End If Next 
  5. VBScript find exact match in text file

    • Description: Find an exact match of a string in a text file using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strContent strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_exact_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) strContent = objFile.ReadAll objFile.Close If InStr(strContent, strSearchString) > 0 Then WScript.Echo "Found exact match: " & strSearchString Else WScript.Echo "Exact match not found." End If 
  6. VBScript find line number of string in text file

    • Description: Retrieve the line number where a specific string appears in a text file using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strLine, lineNumber strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_string_to_search" lineNumber = 0 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) Do Until objFile.AtEndOfStream lineNumber = lineNumber + 1 strLine = objFile.ReadLine If InStr(strLine, strSearchString) > 0 Then WScript.Echo "String found at line: " & lineNumber Exit Do End If Loop objFile.Close 
  7. VBScript find and replace string in text file

    • Description: Search for a string in a text file and replace it using VBScript.
    • Code:
      Const ForReading = 1 Const ForWriting = 2 Dim strFilePath, strSearchString, strReplaceString, objFSO, objFile, strContent strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "string_to_search" strReplaceString = "replacement_string" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) strContent = objFile.ReadAll objFile.Close strContent = Replace(strContent, strSearchString, strReplaceString) Set objFile = objFSO.OpenTextFile(strFilePath, ForWriting) objFile.Write strContent objFile.Close 
  8. VBScript search file contents for string

    • Description: Search the entire contents of a file for a specific string using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strContent strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) strContent = objFile.ReadAll objFile.Close If InStr(strContent, strSearchString) > 0 Then WScript.Echo "Found in file contents: " & strSearchString Else WScript.Echo "String not found in file contents." End If 
  9. VBScript search for specific text in text file

    • Description: Implement a function to search for a specific text in a text file using VBScript.
    • Code:
      Const ForReading = 1 Dim strFilePath, strSearchString, objFSO, objFile, strLine strFilePath = "C:\Path\To\Your\File.txt" strSearchString = "your_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFilePath, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine, strSearchString) > 0 Then WScript.Echo "Found in line: " & strLine Exit Do End If Loop objFile.Close 
  10. VBScript find exact match in multiple text files

    • Description: Search for an exact match of a string in multiple text files using VBScript.
    • Code:
      Const ForReading = 1 Dim strFolder, strSearchString, objFSO, objFile, strLine strFolder = "C:\Path\To\Your\Folder\" strSearchString = "your_exact_string_to_search" Set objFSO = CreateObject("Scripting.FileSystemObject") For Each objFile In objFSO.GetFolder(strFolder).Files If objFSO.GetExtensionName(objFile.Path) = "txt" Then Set objFile = objFSO.OpenTextFile(objFile.Path, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine, strSearchString) > 0 Then WScript.Echo "Found in file: " & objFile.Path Exit Do End If Loop objFile.Close End If Next 

More Tags

dql thumbnails grep py-amqplib in-app-purchase c11 aws-sdk slidetoggle tsconfig angular-reactive-forms

More Programming Questions

More Transportation Calculators

More General chemistry Calculators

More Internet Calculators

More Pregnancy Calculators