Page 1 of 1

VBScript to longest line length

Posted: Mon Jul 22, 2013 10:40 am
by DataMystic Support
Below is a .vbs script to find the length/size of the longest line in a file, without breaking it up into lines first.

If you have a 6GB file that might have a 2GB line somewhere, this code will find it!

Code: Select all

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\aemail\2005.txt", ForReading)

maxLength = 0


Do Until objTextFile.AtEndOfStream

    strCharacters = objTextFile.Read(16384)
    for i = 1 to len(strCharacters)
	test = Mid(strCharacters,i,1)
        if (test = chr(13)) or (test = chr(10)) then
	    if count > maxLength then
		maxLength = count
'		WScript.Echo "New longest line: " & maxLength
	    end if
	    count = 0
	else
	    count = count + 1
	end if
    Next
Loop

objTextFile.Close

WScript.Echo "Longest line was: " & maxLength