How to find the longest row in a file ?

How to find the longest row in a file ?

2
NewbieNewbie
2

    Dec 14, 2005#1

    Hi,

    I am looking for days for a macro that will loop all rows of a text file and find the longest one.

    For instance I have a file with 100000 rows like
    100000 | test | data bla bla
    100001 | test | data
    100002 | test | data more bla bla
    100003 | test | data bla bla

    I want to create a macro that will return or select the row
    100002 | test | data more bla bla

    Any idea ?
    Thanks

    Note : I am using UE 9.10

    344
    MasterMaster
    344

      Dec 14, 2005#2

      Hi Xavier,

      I think you won't get lucky with UE-macros here, cause you need a variable here (or a special trick I don't know), but there is a workaround.

      Take this vb-Script "longestline.vbs":

      Code: Select all

      Dim objFileSystem, objInputFile
      Dim strOutputFile, inputData, strData, strLine, i
      
      Const MYFILENAME = "c:\test.txt"
      
      Const OPEN_FILE_FOR_READING = 1
      
      Set objFileSystem = CreateObject("Scripting.fileSystemObject")
      Set objInputFile = objFileSystem.OpenTextFile(MYFILENAME , OPEN_FILE_FOR_READING)
      
      inputData = Split(objInputFile.ReadAll, vbNewline)
      
      i=0
      For each strData In inputData
          'WScript.Echo strData & len(strData)
          if len(strData) > i then
             i = len(strData)
             strLine = strData
          end if
      Next
      
      objInputFile.Close
      Set objFileSystem = Nothing
      
      WScript.Echo  "Longest line is: " & strLine
      
      WScript.Quit(0)
      
      and use your filename for MYFILENAME.
      You can customize this script EASILY with filename as parameter so you can call it via UE-toolbar (!).

      rds Bego
      Normally using all newest english version incl. each hotfix. Win 10 64 bit

      2
      NewbieNewbie
      2

        Dec 15, 2005#3

        Thanks a lot Bego.

        It works well and it's fast (500000 rows analysed in 10s).

        I did a version that I can call from UE:

        Command Line: MaxRow.vbs %F
        Working Directory: %p
        Menu Item Name: Get max row

        Here is the version of MaxRow.vbs:

        Code: Select all

        Dim objFileSystem, objInputFile
        Dim strOutputFile, inputData, strData, strLine, i
        Dim ArgObj, strFilename
        
        Const OPEN_FILE_FOR_READING = 1
        
        Set ArgObj = WScript.Arguments 
        strFilename = ArgObj(0) 
        
        Set objFileSystem = CreateObject("Scripting.fileSystemObject")
        Set objInputFile = objFileSystem.OpenTextFile(strFilename , OPEN_FILE_FOR_READING)
        
        inputData = Split(objInputFile.ReadAll, vbNewline)
        
        i=0
        For each strData In inputData
            'WScript.Echo strData & len(strData)
            if len(strData) > i then
               i = len(strData)
               strLine = strData
            end if
        Next
        
        objInputFile.Close
        Set objFileSystem = Nothing
        
        WScript.Echo  "Longest line is: " & strLine & vbCrlf & vbCrlf & "Nb characters: " & Len(strLine)
        
        WScript.Quit(0)
        

        344
        MasterMaster
        344

          Dec 15, 2005#4

          Hi Xavier,

          jup, thats what I meant :idea:

          Anyway, one hast to say that this solution is limited to PC-files. :?
          If working on a remote FTP-file, it fails.
          (But I dont care about it :wink:

          Have fun, Bego
          Normally using all newest english version incl. each hotfix. Win 10 64 bit