' FindLineNo.vbs ' VBScript program to display a specified line in a specified file. ' Provide the name of the file and the line number. The program displays ' the line just before the one specified, the specified line, and the ' line immediately after the one specified. This helps determine which ' line raised an error when all you have is the line number. ' ' ---------------------------------------------------------------------- ' Copyright (c) 2011 Richard L. Mueller ' Hilltop Lab web site - http://www.rlmueller.net ' Version 1.0 - May 21, 2011 ' ' You have a royalty-free right to use, modify, reproduce, and ' distribute this script file in any way you find useful, provided that ' you agree that the copyright owner above has no warranty, obligations, ' or liability for such use. Option Explicit Dim strFile, objFSO, objFile, strLine, intCount, blnFound, intLine Dim strPrevious Const ForReading = 1 ' Two arguments required. If (Wscript.Arguments.Count <> 2) Then Wscript.Echo "Two arguments required, a file name and a line number." Wscript.Echo "If the file is not in the current folder, include a path." Wscript.Echo "If the name or path includes spaces, enclose in quotes." Wscript.Echo "Syntax:" Wscript.Echo "cscript //nologo FindLineNo.vbs ""c:\My Scripts\Example.vbs"" 47" Wscript.Quit End If ' Retrieve arguments. strFile = Wscript.Arguments(0) intCount = CInt(Wscript.Arguments(1)) ' Open the file for reading. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Read the file to find the specfied line. intLine = 0 strPrevious = "" blnFound = False Do Until objFile.AtEndOfStream strLine = objFile.ReadLine intLine = intLine + 1 If (intLine = intCount) Then If (intCount > 1) Then ' Display the line before the one specified. Wscript.Echo Right("000" & CStr(intCount - 1), 3) &": " & strPrevious End If ' Display the specified line. Wscript.Echo Right("000" & CStr(intCount), 3) & ": " & strLine If (objFile.AtEndOfStream = False) Then ' Display the line after the one specified. Wscript.Echo Right("000" & CStr(intCount + 1), 3) &": " & objFile.ReadLine End If ' No need to read any more of the file. blnFound = True Exit Do End If ' Keep track of the previous line. strPrevious = strLine Loop objFile.Close If (blnFound = False) Then Wscript.Echo "No line number " & CStr(intCount) & " found" End If