Text manipulation in VB?

Discussion in 'Software' started by RexNoctis, Oct 18, 2006.

  1. RexNoctis

    RexNoctis Corporal

    Hi there,

    At first glance, this seems easy but I'm looking for ideas of how to implement this. I have a text file that I need to pull some data from, the file looks like this:

    Now, it's this bit at the bottom that I'm having trouble with:

    I need to pull out the numbers that are in red. This is fairly easy to do with MID() etc. My problem is that the figures are not always in the same place on the line. The only constant is the 'MVF' and 'MVFINDX' at the beginning of the line. My current solution is to grab a larger chunk of text than I need and trim the blank space. Horrible bodge job and although it was working for a while, recenlty it has come too far out of line. It also doesn't allow for any changes in the future.

    At this stage, I'm looking more for ideas than a full solution. I've included the code below:

    Code:
            '############################ Get statreport.result from DBS01 #######################
    
    
    
            Dim TempMVF As String
            Dim TempMVFI As String
            Try
                My.Computer.FileSystem.DeleteFile(PACSTempFile)
            Catch ex As Exception
            End Try
    
            Try
                My.Computer.Network.DownloadFile(PACSServerFTP, PACSTempFile, PACSFTPPACSOracleServerUsername, PACSFTPPACSOracleServerPassword)
            Catch ex As Exception
                Dim Error_SW As New StreamWriter(ErrorLogFile, True)
                Error_SW.WriteLine("PACS FTP Error: " & Now & " " & ex.Message)
                listErrors.Items.Add("PACS FTP Error - " & Now)
                Error_SW.Close()
            End Try
    
            Try
                Dim sr As New StreamReader(PACSTempFile)
                PACSTempFile = sr.ReadToEnd
                sr.Close()
                UpdateProgress.Value = 70
    
    
                TempMVF = Mid(PACSTempFile, 2391, 79)
                TempMVFI = Mid(PACSTempFile, 2838, 79)
    
                MVFWeeks = Microsoft.VisualBasic.Right(TempMVF, 5)
                MVFFree = Trim(Mid(TempMVF, 15, 9))
                MVFUsed = Trim(Mid(TempMVF, 24, 9))
                MVFIWeeks = Microsoft.VisualBasic.Right(TempMVFI, 6)
                MVFIFree = Trim(Mid(TempMVFI, 15, 9))
                MVFIUsed = Trim(Mid(TempMVFI, 24, 9))
    
                UpdateProgress.Value = 80
    
            Catch ex As Exception
                MVFWeeks = 0
                MVFFree = 0
                MVFUsed = 0
                MVFIWeeks = 0
                MVFIFree = 0
                MVFIUsed = 0
    
                Dim Error_SW As New StreamWriter(ErrorLogFile, True)
                Error_SW.WriteLine("PACS File Error: " & Now & " " & ex.Message)
                listErrors.Items.Add("PACS File Error - " & Now)
                Error_SW.Close()
            End Try
    
            UpdateProgress.Value = 90
    
    Thanks all!
     
  2. matt.chugg

    matt.chugg MajorGeek

    Are there always the same amount of numbers on the line (IE space delimited) ?

    Code:
            [COLOR=#0000FF]Dim[/COLOR] fs [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]New[/COLOR] System.IO.FileStream([COLOR=#FF0000]"c:\ttt.txt"[/COLOR], IO.FileMode.Open)
            [COLOR=#0000FF]Dim[/COLOR] sr [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]New[/COLOR] System.IO.StreamReader(fs)
    
            [COLOR=#0000FF]Dim[/COLOR] MVFNumbers(2) [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR]
            [COLOR=#0000FF]Dim[/COLOR] MVFINDXNumbers(2) [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR]
    
            Do [COLOR=#0000FF]While[/COLOR] sr.Peek <> -1
                [COLOR=#0000FF]Dim[/COLOR] line [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR] = sr.ReadLine
                
                [COLOR=#0000FF]If[/COLOR] line.StartsWith([COLOR=#FF0000]"MVF "[/COLOR]) And line.IndexOf([COLOR=#FF0000]"/"[/COLOR]) = -1 [COLOR=#0000FF]Then[/COLOR]
                    MVFNumbers(0) = line.Split([COLOR=#FF0000]" "[/COLOR])(1)
                    MVFNumbers(1) = line.Split([COLOR=#FF0000]" "[/COLOR])(2)
                    MVFNumbers(2) = line.Split([COLOR=#FF0000]" "[/COLOR])(5)
                [COLOR=#0000FF]Else[/COLOR]
                    [COLOR=#0000FF]If[/COLOR] line.StartsWith([COLOR=#FF0000]"MVFINDX"[/COLOR]) And line.IndexOf([COLOR=#FF0000]"/"[/COLOR]) = -1 [COLOR=#0000FF]Then[/COLOR]
                        MVFINDXNumbers(0) = line.Split([COLOR=#FF0000]" "[/COLOR])(1)
                        MVFINDXNumbers(1) = line.Split([COLOR=#FF0000]" "[/COLOR])(2)
                        MVFINDXNumbers(2) = line.Split([COLOR=#FF0000]" "[/COLOR])(5)
                    [COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]If[/COLOR]
                [COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]If[/COLOR]
            [COLOR=#0000FF]Loop[/COLOR]
    
            MsgBox([COLOR=#FF0000]"MVF"[/COLOR] & [COLOR=#FF0000]"        "[/COLOR] & MVFNumbers(0) & [COLOR=#FF0000]" - "[/COLOR] & MVFNumbers(1) & [COLOR=#FF0000]" - "[/COLOR] & MVFNumbers(2) & vbCrLf & _
                   [COLOR=#FF0000]"MVFINDX"[/COLOR] & [COLOR=#FF0000]"    "[/COLOR] & MVFINDXNumbers(0) & [COLOR=#FF0000]" - "[/COLOR] & MVFINDXNumbers(1) & [COLOR=#FF0000]" - "[/COLOR] & MVFINDXNumbers(2) & vbCrLf)
    
            sr.Close()
            fs.Close()
    
    Maybe something like that will work for you.

    It reads in each line one at a time and checks if it begins with the right word (but doesn't contain a slash) then split the line into an array of 'words' (ie space delimited string) then just pull out the right numbers.

    You could also use infedof(" ") to find the locations of the spaces.

    WOuld need some error trapping and I like to put file read/writes in seperate threads to keep everything running smoothly.
     
  3. RexNoctis

    RexNoctis Corporal

    Matt, that is excellent!

    There is always the same amount of numbers (space delimited) on the line unless a warning is thrown, like on the TEMP line.

    Never even thought of reading it by line, not sure why, just a blind spot I guess. I was working on counting the number of times 'MVF' appeared!

    Many Thanks for your help!

    Cheers,

    Danny.
     
  4. matt.chugg

    matt.chugg MajorGeek

    No probs, theres lots of ways to do what you want and what I just did may not be the best but it does work! Maybe Kodo will have some input too if he's around.
     
  5. Kodo

    Kodo SNATCHSQUATCH

    Regular Expressions baby!!
     
  6. matt.chugg

    matt.chugg MajorGeek

    Pah lol, I should have thought of that, i've never been very good with though :(
     
  7. Kodo

    Kodo SNATCHSQUATCH

    you should try using it more often.. they are simply the best thing since sliced bread ! :D
     
  8. matt.chugg

    matt.chugg MajorGeek

    I should! The split function is probably better than the native split (the one I used earlier). I think a lot is still coming through from the old vb6 days lol
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds