Announcement

Collapse
No announcement yet.

search inside a file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • search inside a file?

    hi,,
    assume i have a text file with many lines of information.
    how can i search the file for characters.
    e.g. I want to find 2222 which is part of SEARCHID2222
    ??

    hints plz?
    thanks

  • #2
    Binary ?

    Open the file in binary mode to a string, the search the string with instr ....
    Warped by the rain, Driven by the snow...

    jimatluv2rescue.com

    Comment


    • #3
      Originally posted by Jim Padgett View Post
      Open the file in binary mode to a string, the search the string with instr ....
      thanks
      but INSTR gives the first occurrence of the character. If i need to look for second occurrence,i have to save the position and start searching again starting from the saved position again?? or there is another method?

      thanks

      Comment


      • #4
        Try the TALLY function instead of the INSTR.
        Rod
        I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

        Comment


        • #5
          Originally posted by Raed Abu-Sanad View Post
          but INSTR gives the first occurrence of the character. If i need to look for second occurrence,i have to save the position and start searching again starting from the saved position again?? or there is another method?
          That's about right. Search for the string using InStr and if found then increment your starting search offet by the length of the search string. Something like (not tested, this is straight from my brain):

          Code:
          nBufferSize = Len(sBuffer)
          nOffSet = 1
          
          Do Until nOffSet > nBufferSize
          
             f = InStr( nOffset, sBuffer, sSearchString )
             If f Then
                ' You've found a match... do what you want to do here
          
                ' increment the offset to keep searching from
                nOffSet = f + Len(sSearchString)
             Else
                Exit Do
             End If
          
          Loop
          Paul Squires
          FireFly Visual Designer (for PowerBASIC Windows 10+)
          Version 3 now available.
          http://www.planetsquires.com

          Comment


          • #6
            If it is a text file, then quite possibly you will want to know some other contents of the line containing the target data, and/or the line number, as well as its absolute position within the file.

            So having loaded the file into a single string, you can use PARSE$ to transform it into a string array, then either search each string in the array, or having established the target position in the single string, subtract the lengths of each string in the array until the line number is known. If the line number is not required, then the text in which the target occurs can be extracted by winding backwards and forwards from the target position to the nearest line delimiter ($CRLF, probably), or to the limits of the string.

            Comment


            • #7
              Chris is correct the various PARSE functions and statements are probably the fastest depending on what you are trying to determine. Here is a very simple example (appologies for the simple variable names). I suspect PARSECOUNT and TALLY from Rodney are basically the same code except PARSECOUNT adds 1 to the count
              Code:
              #COMPILE EXE
              
              FUNCTION PBMAIN () AS LONG
              
                  f& = FREEFILE
                  OPEN "textfile" FOR BINARY AS f&
                  GET$ f$, LOF(f&), t$    'convert the whole file to a single string t$
                  c& = PARSECOUNT(t$, "2222") 'returns the number of occurances plus 1
                  REDIM ta(1 TO c&) AS STRING
                  PARSE t$, ta$(), "2222" 'will give an array of strings broken by each
                  'occurance of 2222 with the 2222 actualy removed
                  'Note the reason the count is plus 1 is because the last count will be for
                  ' the remainder of the string or an empty string if the file ennded with 2222
              
              END FUNCTION
              Last edited by John Petty; 10 Oct 2008, 10:51 AM.

              Comment


              • #8
                Here's a sample for the TALLY command, rather than this being inside a file it's a string with several occurances of a string you may be looking for.

                In my case I use the | pipe symbol to separate files from a directory search and then the calling function parses those..

                Local RemoteDir as string
                local RemoteFiles as string
                local FileCount as long
                Local lLoop as long

                RemoteDir = "\\SERVERNAME\D$\EXPORT FILES"
                RemoteFiles = CCSGetDirListing(RemoteDir)

                'RemoteFiles may look like "Text1.txt|Txt2.txt|txt4.txt|"

                FileCount = Tally(RemoteFiles,"|")
                For lLoop = 1 to FileCount
                FileSpec = Parse$(RemoteFiles,"|",lLoop)
                Do whatever to string/file
                Next


                Function CCSGetDirListing(InputDir As String) Export As String
                Local lCount As Long
                Local lLoop As Long
                Local FileSpec As String
                Local Extension As String
                Local FileList As String
                Local f As Asciiz * %MAX_PATH
                Local FindData As WIN32_FIND_DATA
                Local hDir As Long

                ChDir InputDir

                Extension = "log"
                FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
                f = "*.*" 'Read all files, filter later
                hDir = FindFirstFile(f, FindData)
                If hDir = %INVALID_HANDLE_VALUE Then
                Function = "Unale to read directory!"
                Exit Function
                End If

                Do
                Select Case Left$(FindData.cFileName,1)
                Case "." 'Ignore these directories
                Case "_" 'Front page directories
                Case Else
                'Verify the extention is what we are looking for:
                FileSpec = FindData.cFileName
                'At this point we have a valid filename
                Incr lCount
                If IsTrue Len(FileList) Then
                FileList = FileList & "|" & FileSpec 'Add Pipe so we can parse on it later
                Else
                FileList = FileSpec
                End If
                End Select
                Loop While FindNextFile(hDir, FindData)
                FindClose hDir
                Function = FileList
                End Function
                Scott Turchin
                MCSE, MCP+I
                http://www.tngbbs.com
                ----------------------
                True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                Comment

                Working...
                X