CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
  1. #16
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Finding strings in multiple files

    Jeez, you make things tough...

    Code:
    ReDim str(5)
    str(0) = "AAAAAA 1.1.1"
    str(1) = "AAAAAB 1.1.1"
    str(2) = "AAAAAC 1.1.1"
    str(3) = "AAAAAA 1.1.2"
    str(4) = "AAAAAB 1.1.2"
    str(5) = "AAAAAA 1.1.2"
      
    
      'str() = Split(strBuff, vbCrLf)
    '  MsgBox "There are " & UBound(str) + 1 & " lines in the file"
      
      Dim words() As String, y As Integer, max2 As String
      max2 = "0.0.0"
      For x = 0 To UBound(str)
        words() = Split(str(x), " ")
         For y = 0 To UBound(words)
           If y = 1 Then
             If words(y) > max2 Then
                max2 = words(y)
                st = st & words(0) & " " & max2 & vbCrLf
             End If
          End If
        Next y
      Next x
      MsgBox st
    Last edited by dglienna; July 26th, 2008 at 11:24 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  2. #17
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Finding strings in multiple files

    My apologies - I had forgotten that . is considered < 1. If you have a seperator which is greater than the numerical value, and ever hit a situation where the strings are of unequal length, things break down a bit...
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #18
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Finding strings in multiple files

    Well, if the newer times are going to be at the end of the file, I think I'd start there. I'd also probably read the entire file into a string array, split on vbCrLf, unless the file was huge.
    Code:
    Dim X() As String, Newest As String
    Dim I As Long, Count As Long
    
    X = Split("AAAAAA 1.1.1,AAAAAB 1.1.1,AAAAAC 1.1.1,AAAAAA 1.1.2,AAAAAB 1.1.2,AAAAAA 1.1.2", ",")
    I = UBound(X)
    Newest = Mid$(X(I), InStrRev(X(I), Chr$(32)))
    X = Filter(X, Newest)
    I = UBound(X)
    
    Do
      Count = Count + 1
      X = Filter(X, X(I), False)
      I = UBound(X)
    Loop While I >= 0
    
    Debug.Print Count
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #19
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Finding strings in multiple files

    Any idea how many entries there might be for the newest time? If that isn't going to be too much, it may work to scan the file backwards until you hit an entry older than the last, then grab everything from that point up to the end.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #20
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Finding strings in multiple files

    Quote Originally Posted by Chrispy360
    Thank you very much for that, unfortunatly, i basically have a file like the following:
    AAAAAA 1.1.1
    AAAAAB 1.1.1
    AAAAAC 1.1.1
    AAAAAA 1.1.2
    AAAAAB 1.1.2
    AAAAAA 1.1.2

    What i wish to do though, is detect the newest times, in this example 1.1.2 and then find out how many different strings there are, for exmaple the newest time in my little example is 1.1.2, then i want it to say there were two different strings, AAAAAA,AAAAAB, but not detect the second AAAAAA, resulting in a textbox on the .exe = 2. Is this possible.
    Thanks
    Ok but the first post said that you needed to search multiple files for a number and find out which file that number was in. What you are asking here is completely different.

    In a later post you said the files are huge so reading the whole file or reading line by line does not make much since as it would be very slow. You also mention somethign about the first 2 characters. So I have a few questions.

    How often is data written to the file?
    Do you need to find the newest entries for each prefix or just the newest entries?
    Are all of the prefixes likely to be written daily?
    Abotu how much data would be written on a daily basis?

  6. #21
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Finding strings in multiple files

    Quote Originally Posted by WizBang
    Any idea how many entries there might be for the newest time? If that isn't going to be too much, it may work to scan the file backwards until you hit an entry older than the last, then grab everything from that point up to the end.
    I'm thinking that it might be a good idea to do a binary read on the file and seek to somewhere near the end then read a block of data say maybe 1k into memory and then search that assuming of course that the data being looked for would always be found int he last 1k of data and of course that block size should be adjusted to whatever size is likely to be needed.

    Assuming this is an onging process that needs to be repeated then I would keep a seperate file, perhaps an ini file that contains the results from the last scan and the byte position of the last read.

  7. #22
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Finding strings in multiple files

    Yes DataMiser, that's basically what I had in mind for the searching. And in the case of an ongoing process, your suggestion to keep track of the byte position (and any other pertinent data) is a good one. Though we don't yet know if more than one timestamp is expected between each run.

    I'm sure the OP can clear up these questions.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #23
    Join Date
    Jul 2008
    Posts
    45

    Re: Finding strings in multiple files

    I have managed to work out a workaround for my problem , but have come accross another hopefully smaller problem.

    By opening the file as Input and a second file for Output I can get the result I need , except there seems to be a limit on either the size of file that VB can read or write to!

    My original file has over 10,000 lines and each line contains about 120-140 characters., but VB will only copy the first 140 lines.


    Is there a way of getting round this?
    Last edited by Chrispy360; July 27th, 2008 at 04:09 PM.

  9. #24
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Finding strings in multiple files

    I am not aware of limits on file sizes in vb and have worked with files that are several megs in size. What code are you using?

  10. #25
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Finding strings in multiple files

    There actually is a limit on file size (the same as in C++), at a little over 4 GB. It doesn't sound like that's the issue here, but GremlinSA has written an article on how to overcome the limitation.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  11. #26
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Finding strings in multiple files

    Quote Originally Posted by WizBang
    There actually is a limit on file size (the same as in C++), at a little over 4 GB. It doesn't sound like that's the issue here, but GremlinSA has written an article on how to overcome the limitation.
    That is also a limit on file size for a fat32 drive as well. I never thought about it being a limit in VB but makes sense must be using 4 bytes to hold the file size.

    I agree though that is not the issue 140bytesx10,000 is far short of 4 gigs more like 1.4 megs

  12. #27
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Finding strings in multiple files

    WizBang -

    Yeah, I forgot if ParamArrays could be declared a specific data type or just variant. I don't use ParamArrays that often, but felt one would be appropriate for this purpose. And I had the function return a variant since I was returning a full array, should I have used object or string?
    Last edited by VehementSoftware; July 28th, 2008 at 01:44 PM.

  13. #28
    Join Date
    Jul 2008
    Posts
    45

    Re: Finding strings in multiple files

    Ignore the filesize error statement. The limitation was caused by errors in the line of text not by file size. Sorry about that , should have checked that first!

  14. #29
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Finding strings in multiple files

    Quote Originally Posted by VehementSoftware
    WizBang -

    Yeah, I forgot if ParamArrays could be declared a specific data type or just variant. I don't use ParamArrays that often, but felt one would be appropriate for this purpose. And I had the function return a variant since I was returning a full array, should I have used object or string?
    A function can both take and return an array. In the case of a string array, the following would do both:
    Code:
    Private Function SearchFiles(ByVal SearchString as String, ByVal FilePaths() as String) as String()
    I can honestly say I've never used ParamArray, and it seems that would require using a Variant.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  15. #30
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Finding strings in multiple files

    Thanx wizbang, that is very helpful. Yeah, actually passing an array would be probably more effective. The calling procedure would look like this
    Code:
    Dim strFilePaths(3) as String
    Dim strFindString as String
    
    strFilePaths(0) = 'path 1
    strFilePaths(1) = 'path 2
    strFilePaths(2) = 'path 3
    
    strFilePaths = SearchString(strFindString, strFilePaths())
    Or something similar...as opposed to
    Code:
    Dim strFile1 as string
    Dim strFile2 as string
    Dim strFile3 as string
    Dim strFindString as string
    Dim strFoundPaths() as string
    
    strFile1 = 'path 1
    strFile2 = 'path 2
    strFile3 = 'path 3
    
    strFoundPaths = SearchString(strFindString, strFile1, strFile2, strFile3)
    Don't know if what I was trying to do makes sense there...but the paramarray let's you list unkown number of parameters. So really it would depend on how the file's paths are stored in the calling prodecure as to what the best function header would be; even though your way would overall be much more effective.

    I've personally only used the paramarray in an Error Logging object I created. I had to use it so I could pass all variable names and values through to the method that writes the error logs; other then that, I have never thought of another purpose for it.
    Last edited by VehementSoftware; July 29th, 2008 at 10:03 AM.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured