'+ ' line_numbers.vbs ' ' A tool to number source lines of big VBScript procedures. ' ' Usage : C:\>cscript /nologo line_numbers.vbs 'MySourceFile'.vbs ' ' Input : Source file to be numbered (any text file format will fit) ' Output : source file name + "_n.vbs" ' Side effects: this proc creates a file named 'MySourceFile'_n.txt in default directory. ' ' Long description and comments: ' This Q&D tool has been written to help VBScript procedures debugging. In a 20 lines source, ' it's not that a problem to locate any offending line. In a 100 and more lines source, it becomes ' pretty boring to find the target line. This tool produces an exact copy of the source file to ' number, with a five digits number at the beginning of each and every line. Then, it calls Notepad ' to open the numbered file. At each usage, the previous version of the "numbered" file is deleted. ' ' Prerequisites: None. ' ' History ' v1.0-0 18-oct-2010 DTL Didier.Morandi at gmail.com http://www.didiermorandi.fr/vbscript ' v1.1-0 10-nov-2010 DTL change output name to: filename_n.vbs ' v1.2-0 15-nov-2010 DTL change output name to: filename_n.txt ' '- ----------------------------------------------------------------------- Option Explicit Dim fso, infile, infilename, outfile, outfilename, inline, outline Dim severity, status, p1, p2, line_nr, data, line_nr_data, WshShell, currDate, currTime Const ForReading = 1, ForWriting = 2, ForAppending = 8, fatal = 1, not_fatal = 0 ' file object creation On Error Resume Next Set fso=CreateObject("Scripting.FileSystemObject") call errmgt(fatal) 'ask for input file if wscript.arguments.count = 0 then wscript.stdout.write "Input file =end: " infilename = wscript.stdin.readline if infilename = "" then wscript.quit 0 else infilename = wscript.arguments(0) end if inFilename = Ucase(inFilename) if not fso.fileExists(infilename) then wscript.echo "File " & infilename & " does not exist." wscript.quit 1 end if ' open source file. If error, abort. On Error Resume Next Set infile = fso.GetFile(infilename) call errmgt(fatal) On Error Resume Next set inline = infile.OpenAsTextStream(ForReading) call errmgt(fatal) outfilename = BuildOutputFileName(infilename) ' open destination file as new file. On Error Resume Next Set outfile = fso.CreateTextFile(outfilename) call errmgt(fatal) 'read/write loop line_nr = 0 Do While Not inline.AtEndOfStream data = inline.ReadLine line_nr = line_nr + 1 if line_nr < 10000 then line_nr = "0" & line_nr 'yes. WMIdiag.vbs is >30.095 lines long... if line_nr < 1000 then line_nr = "0" & line_nr if line_nr < 100 then line_nr = "0" & line_nr if line_nr < 10 then line_nr = "0" & line_nr data = line_nr & ": " & data outfile.WriteLine(data) Loop data = "-----" outfile.WriteLine(data) currDate = Date currTime = Time data = "Listing provided by Line_numbers.vbs on " & currDate & " " & currTime outfile.WriteLine(data) data = "(c) 2010 Didier.Morandi (at) gmail.com" outfile.WriteLine(data) data = "http://www.scripting-errors.com" outfile.WriteLine(data) inline.Close outfile.Close wscript.echo line_nr & " lines written to " & outfilename on error goto 0 Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "%windir%\notepad " & outfilename wscript.quit 0 '------------------------------------------------------------------------ Function BuildOutputFileName(strInfile) Dim strInFileName strInFileName = left(strInFile, len(strInFile) - 4) & "_n.txt" BuildOutputFileName = strInFileName end Function '------------------------------------------------------------------------ '+ ' errmgt.vbs ' ' Error handling management routine to be added at the end of a VBscript. ' Usage: Call errmgt(severity) ' Two params should be defined in the caller source: severity and status. ' If error is not fatal, calling code execution is resumed, otherwise procedure stops. ' Note: caller script should be called via Cscript instead of Wscript. ' ' v1.0-0 14-oct-2010 DTL Didier.Morandi at gmail dot com '- sub errmgt(severity) status = err.number if status <> 0 then if severity = 0 then wscript.echo "VBScript-E-Error, " & err.source wscript.echo "Err " & status & " line ? : " & err.description & vbcrlf else wscript.echo "VBScript-F-Error, " & err.source wscript.echo "Err " & status & " line ? : " & err.description wscript.quit 1 end if end if end sub '------------------------------------------------------------------------ 'eof errmgt.vbs