dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] Read Text file and make changes to text by searching a string

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Resolved [RESOLVED] Read Text file and make changes to text by searching a string

    Hi Friends
    I ve come up for a help in modifying the codes of existing VB6 project of reading a file and make changes in the file.

    In my previous post similar to this project you helped out me by resolving this by using codes at post http://www.vbforums.com/showthread.p...5&goto=newpost

    1. Requirement earlier was reading a text file and searching for string EC1 and make modification to the third column value by adding the value from text1.text.
    file contents as sample are below

    MSG 1 56570.229 SBG_ECOM_LOG_SHIP_MOTION_0 -0.18
    MSG 1 56570.243 SBG_ECOM_LOG_EKF_EULER -0.05, 0.12, 2.63
    EC1 2 56570.243 5.240
    HCP 1 56570.243 0.181 -3.099 7.015
    MSG 1 56570.249 SBG_ECOM_LOG_SHIP_MOTION_0 -0.18

    code used was as below and worked fine.

    Code:
    For intI = 0 To UBound(strRecords)
        If strRecords(intI) <> "" Then
            intPos = InStr(strRecords(intI), "EC1")
            If intPos > 0 Then
                strFields = Split(strRecords(intI), " ")
                If UBound(strFields) > 2 Then
                    dblVal = CDbl(strFields(3))
                    If IsNumeric(Text1.Text) Then
                        dblVal = dblVal + CDbl(Text1.Text)
                    Else
                        dblVal = dblVal + 0#
                    End If
                    strFields(3) = Format(dblVal, "#0.##0")
                    strRecords(intI) = Join(strFields, " ")
                End If
            End If
            '
            ' Write the record to the output file
            '
            Print #intFileOut, strRecords(intI)
        End If
    here the codes splitted the values in and later selected the 3rd string. This i used for using multiple files by using the open folder and select file list option.

    for single file option friend from forum helped with code is as below (just for reference)

    Code:
    Private Sub cmdCreateOutput_Click()
    Dim strFileToProcess As String
    With List1
        If .ListCount > 0 Then
            For intI = .ListCount - 1 To 0 Step -1
                strFileToProcess = Mid$(Label1.Caption, InStr(Label1.Caption, ": ") + 2) & .List(intI)
                Call ProcessFile(strFileToProcess)
                .RemoveItem intI
            Next intI
            .AddItem "Files have been Processed"
        End If
    End With
    End Sub
    
    Private Sub cmdGetFileNames_Click()
    Dim strFileNames() As String
    Dim i As Integer
    Dim intI As Integer
    strFileNames = Split(GetFiles, Chr(0))
    With List1
        If UBound(strFileNames) <> -1 Then
            .Clear
            If UBound(strFileNames) = 0 Then
                Label1.Caption = "Files selected from: " & Mid$(strFileNames(0), 1, InStrRev(strFileNames(0), "\"))
                strFileNames(0) = Mid$(strFileNames(0), InStrRev(strFileNames(0), "\") + 1)
                .AddItem strFileNames(0)
            Else
                'If more than one file is selected the Path is stored in index 0 of array files in index 1...
                If Mid$(strFileNames(0), Len(strFileNames(0))) <> "\" Then strFileNames(0) = strFileNames(0) & "\"
                Label1.Caption = "Files selected from: " & strFileNames(0)
                For i = 1 To UBound(strFileNames)
                    .AddItem strFileNames(i)
                Next
            End If
        Else
            Label1.Caption = "No Files Selected"
        End If
    End With
    End Sub
    
    Private Sub ProcessFile(strFile As String)
    Dim intFileIn As Integer
    Dim intFileOut As Integer
    Dim intI As Integer
    Dim intPos As Integer
    Dim strData As String
    Dim dblVal As Double
    Dim strOFile As String
    Dim strRecords() As String
    Dim strFields() As String
    '
    ' Allocate a File Number, open, read the entire contents and close the file
    '
    intFileIn = FreeFile
    Open strFile For Input As intFileIn
    strData = Input(LOF(intFileIn), intFileIn)
    Close intFileIn
    '
    ' Create a name for the output file
    '
    intPos = InStrRev(strFile, ".")
    If intPos > 0 Then
        strOFile = Mid$(strFile, 1, intPos - 1) & "_OUT" & Mid$(strFile, intPos)
    Else
        strOFile = strFile & "_OUT"
    End If
    '
    ' Open the output file
    '
    intFileOut = FreeFile
    Open strOFile For Output As intFileOut
    '
    ' Split the input data into records
    '
    strRecords = Split(strData, vbNewLine)
    '
    ' Process each non-null record
    ' If it's a record type of EC1 then split it into fields
    ' add whatever is in text2.text to the 3rd field
    ' replace the original value with the new value
    '
    For intI = 0 To UBound(strRecords)
        If strRecords(intI) <> "" Then
            intPos = InStr(strRecords(intI), "EC1")
            If intPos > 0 Then
                strFields = Split(strRecords(intI), " ")
                If UBound(strFields) > 2 Then
                    dblVal = CDbl(strFields(3))
                    If IsNumeric(Text2.Text) Then
                        dblVal = dblVal + CDbl(Text2.Text)
                    Else
                        dblVal = dblVal + 0.5
                    End If
                    strFields(3) = Format(dblVal, "#0.##0")
                    strRecords(intI) = Join(strFields, " ")
                End If
            End If
            '
            ' Write the record to the output filer
            '
            Print #intFileOut, strRecords(intI)
        End If
    Next intI
    Close intFileOut
    End Sub
    
    Public Function GetFiles(Optional ByVal sTitle As String = "Open files...") As String
    ' sTitle: Optional Title of Dialog
    Dim sFilenames As String
    ' Get the desired name using the common dialog
    Set cdlOpen = CreateObject("MSComDlg.CommonDialog")
    ' set up the file open dialog file types
    With cdlOpen
        .CancelError = False
        .Filter = "Raw Files (*.raw)|*.raw|All Files (*.*)|*.*"
        .FilterIndex = 1
        .DialogTitle = sTitle
        ' set up Common Dialog flags
        ' same as .Flags = cdlOFNHideReadOnly Or cdlOFNPathMustExist Or cdlOFNLongNames Or cdlOFNAllowMultiselect or cdlOFNExplorer
        .Flags = &H4 Or &H800 Or &H40000 Or &H200 Or &H80000
        .ShowOpen
        ' get the selected name
        sFilenames = .FileName
    End With
    GetFiles = sFilenames
    Set cdlOpen = Nothing
    End Function
    main content or the part i need modification is as in the first codes other part of calling file and printing file remains same.

    now situation is that there is modification in the text file and the value to be changed is not the 3rd value but the lines below the string EC1, and all the values need to be modified using the same value from text1.text (for example 0.5) until it find the value -90.000 and continue search next string EC1 until end of file.

    sample text file (part of it ) mentioned below and highlighted red is the values to be modified. can someone help in making changes in code to resolve this.?

    HCP 1 30776.475 -0.03 -5.46 6.83
    HCP 1 30776.490 -0.03 -5.54 6.84
    HCP 1 30776.503 -0.03 -5.57 6.83
    SNR 2 30776.557 11426 93 5 18 220 450 380 29
    EC1 2 30776.557 3 0 5b01 512 1544.52 11426
    13.10 12.68 12.57 12.71 12.62 12.58 11.88 12.64 12.54 11.72 11.67 11.62 11.56 11.51 11.46 11.41 11.36 11.31 11.26 11.21 11.16 11.10 11.06 11.01 10.96 10.90 13.10 10.81 10.77 10.72 10.67 10.63 10.57 10.52 10.48 10.42 10.38 10.34 10.29 10.25 10.20 10.15 10.11 10.06 10.01 9.96 9.91 9.87 9.83 9.79 9.75 9.71 9.66 9.63 9.58 9.54 9.49 9.44 9.40 9.36 9.32 9.29 9.25 9.21 9.17 9.12 9.08 9.04 9.01 8.97 8.94 8.89 8.85 8.81 8.78 8.74 8.71 8.67 8.63 8.59 8.55 8.51 8.47 8.44 8.40 8.37 8.33 8.29 8.26 8.23 8.18 8.15 8.11 8.08 8.04 8.01 7.98 7.95 7.91 7.89 7.86 7.84 7.83 7.80 7.74 7.71 7.70 7.67 7.63 7.61 7.59 7.56 7.53 7.50 7.49 7.45 7.43 7.39 7.39 7.36 7.34 7.32 7.29 7.27 7.25 7.22 7.18 7.16 7.16 7.15 7.13 7.11 7.09 7.08 7.05 7.03 7.01 6.99 6.98 6.97 6.95 6.93 6.93 6.93 6.90 6.87 6.86 6.85 6.83 6.83 6.83 6.82 6.82 6.80 6.79 6.78 6.77 6.74 6.75 6.74 6.74 6.73 6.72 6.73 6.73 6.71 6.69 6.69 6.70 6.69 6.69 6.68 6.70 6.69 6.69 6.69 6.67 6.70 6.71 6.71 6.71 6.71 6.72 6.73 6.74 6.74 6.73 6.72 6.74 6.75 6.76 6.76 6.78 6.79 6.79 6.79 6.80 6.82 6.83 6.84 6.86 6.88 6.88 6.89 6.91 6.92 6.94 6.96 6.98 6.99 7.01 7.01 7.04 7.06 7.07 7.09 7.11 7.14 7.16 7.17 7.19 7.21 7.24 7.27 7.29 7.32 7.34 7.36 7.38 7.41 7.43 7.46 7.49 7.51 7.52 7.55 7.58 7.61 7.65 7.68 7.70 7.74 7.78 7.80 7.83 7.86 7.89 7.91 7.94 7.98 8.02 8.04 8.07 8.11 8.15 8.17 8.21 8.25 8.28 8.31 8.36 8.39 8.44 8.49 8.55 8.60 8.62 8.66 8.69 8.74 8.77 8.82 8.85 8.89 8.93 8.99 9.03 9.08 9.13 9.17 9.22 9.26 9.29 9.34 9.38 9.43 9.48 9.53 9.59 9.64 9.68 9.75 9.81 9.89 9.93 9.98 10.04 10.08 10.14 10.19 10.24 10.34 12.36 12.37 12.41 12.42 12.44 12.47 12.50 12.52 13.21 10.92 11.00 11.09 11.17 11.25 11.31 11.33 12.55 12.59 12.50 12.60 12.68 12.73 12.70 12.74 12.40 12.41 12.49 12.59 12.69 12.81 12.93 13.05 13.17 13.24 13.31 13.39 13.44 13.49 13.52 13.55 13.60 13.58 13.58 13.56 13.55 13.52 13.49 13.46 13.45 13.43 13.40 13.34 13.33 13.32 13.26 13.22 13.20 13.17 13.14 13.11 13.07 13.06 13.05 13.03 13.01 12.99 12.97 12.95 12.90 12.85 12.84 12.81 12.81 12.78 12.76 12.74 12.73 12.70 12.67 12.63 12.60 12.60 12.58 12.58 12.56 12.55 12.54 12.50 12.49 12.47 12.45 12.44 12.40 12.38 12.36 12.34 12.33 12.33 12.31 12.31 12.27 12.25 12.23 12.22 12.21 12.20 12.19 12.18 12.16 12.15 12.13 12.13 12.12 12.11 12.11 12.11 12.07 12.06 12.03 12.01 12.00 12.00 11.99 11.97 11.96 11.95 11.93 11.93 11.92 11.90 11.89 11.88 11.88 11.88 11.87 11.86 11.85 11.84 11.84 11.84 11.82 11.82 11.81 11.80 11.77 11.76 11.75 11.74 11.74 11.73 11.72 11.73 11.73 11.72 11.72 11.72 11.70 11.69 11.69 11.69 11.69 11.69 11.69 11.69 11.70 11.69 11.69 11.68 11.67 11.65 11.64 11.63 11.60 11.58 11.55 11.54 11.50 11.49 11.50 11.46 11.47 11.45 11.44 11.45 11.43 11.43 11.43 11.42 11.41 11.41 11.40 11.40 11.40 11.40 11.41 11.41 11.40 11.40 11.40 11.39 11.39 11.39 11.40 11.39 11.38 11.39 11.38 11.37 11.37 11.37
    57.779 57.610 57.449 57.279 57.119 56.959 56.789 56.619 56.449 56.269 56.099 55.929 55.759 55.579 55.409 55.229 55.049 54.870 54.689 54.499 54.319 54.129 53.949 53.759 53.570 53.369 53.209 52.979 52.789 52.589 52.389 52.189 51.989 51.789 51.580 51.229 51.019 50.809 50.599 50.389 50.169 49.959 49.739 49.519 49.299 49.079 48.849 48.629 48.399 48.169 47.939 47.709 47.479 47.239 46.999 46.759 46.519 46.279 46.040 45.789 45.539 45.289 45.039 44.779 44.529 44.269 44.009 43.749 43.490 43.219 42.949 42.679 42.409 42.139 41.859 41.589 41.309 41.029 40.739 40.459 40.169 39.889 39.599 39.309 39.009 38.719 38.419 38.109 37.809 37.509 37.199 36.889 36.579 36.259 35.949 35.629 35.309 34.989 34.659 34.329 34.009 33.679 33.339 33.010 32.670 32.330 31.990 31.650 31.299 30.949 30.609 30.249 29.900 29.550 29.189 28.829 28.469 28.099 27.740 27.369 27.000 26.629 26.259 25.879 25.509 25.130 24.749 24.359 23.979 23.589 23.210 22.820 22.429 22.029 21.639 21.239 20.849 20.449 20.049 19.650 19.239 18.839 18.429 18.019 17.610 17.199 16.789 16.379 15.969 15.549 15.129 14.720 14.299 13.879 13.459 13.039 12.619 12.189 11.769 11.350 10.919 10.500 10.069 9.639 9.219 8.789 8.359 7.929 7.500 7.069 6.639 6.209 5.790 5.359 4.929 4.499 4.070 3.639 3.210 2.779 2.350 1.919 1.489 1.069 0.639 0.210 0.210 0.639 1.060 1.489 1.910 2.329 2.760 3.179 3.599 4.020 4.430 4.849 5.269 5.679 6.100 6.510 6.920 7.329 7.739 8.149 8.550 8.960 9.359 9.759 10.159 10.559 10.959 11.349 11.749 12.139 12.529 12.920 13.309 13.690 14.080 14.459 14.839 15.220 15.589 15.969 16.339 16.709 17.079 17.449 17.809 18.179 18.539 18.900 19.250 19.609 19.959 20.309 20.660 21.009 21.349 21.699 22.039 22.379 22.709 23.049 23.380 23.710 24.040 24.359 24.690 25.010 25.329 25.650 25.959 26.279 26.590 26.899 27.209 27.510 27.819 28.120 28.420 28.710 29.019 29.309 29.600 29.890 30.180 30.460 30.750 31.030 31.310 31.579 31.859 32.129 32.399 32.670 32.939 33.199 33.460 33.729 33.989 34.239 34.499 34.749 34.999 35.249 35.500 35.749 35.989 36.240 36.479 36.719 36.949 37.189 37.420 37.650 37.880 38.110 38.339 38.560 38.620 38.840 39.059 39.279 39.499 39.709 39.920 40.140 40.339 40.579 40.769 40.979 41.179 41.389 41.589 41.789 41.979 42.169 42.369 42.559 42.750 42.939 43.129 43.310 43.509 43.699 43.880 44.059 44.239 44.409 44.589 44.769 44.950 45.120 45.290 45.470 45.640 45.810 45.970 46.140 46.310 46.469 46.639 46.799 46.959 47.119 47.280 47.440 47.589 47.749 47.900 48.059 48.209 48.360 48.509 48.659 48.810 48.949 49.099 49.239 49.389 49.529 49.669 49.809 49.949 50.089 50.230 50.369 50.500 50.639 50.769 50.900 51.039 51.169 51.300 51.429 51.559 51.689 51.819 51.949 52.070 52.200 52.320 52.439 52.559 52.679 52.799 52.919 53.039 53.159 53.279 53.389 53.509 53.619 53.739 53.849 53.959 54.069 54.189 54.299 54.409 54.509 54.619 54.729 54.839 54.939 55.049 55.150 55.260 55.360 55.459 55.559 55.669 55.769 55.869 55.969 56.060 56.160 56.260 56.359 56.449 56.549 56.639 56.740 56.830 56.929 57.019 57.109 57.199 57.290 57.379 57.469 57.559 57.649 57.740 57.829 57.919 57.999 58.090 58.169 58.269 58.349 58.440 58.519 58.609 58.689 58.770 58.849 58.929 59.009 59.099 59.179 59.249 59.330 59.409 59.489 59.570 59.639 59.719 59.800 59.869 59.949 60.020 60.099 60.169 60.250 60.319 60.389 60.470 60.539 60.609 60.679 60.749 60.820 60.889 60.959 61.030 61.099 61.169 61.239 61.299 61.370 61.439 61.509 61.569 61.639 61.699 61.769 61.830 61.899 61.959 62.029 62.089 62.149 62.219 62.280 62.339 62.400 62.459 62.529 62.589 62.649 62.709 62.769 62.829 62.889 62.949 62.999 63.059 63.119

    -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000
    322 122 202 156 194 162 417 255 170 477 512 494 476 428 415 423 462 487 553 527 508 362 330 523 530 536 242 518 535 500 425 428 377 695 745 704 617 522 607 591 513 585 658 1060 1151 1151 1082 1045 881 764 920 1195 1362 1384 1228 780 1190 1488 1571 1446 1121 920 998 1043 1049 1204 1172 906 667 1122 1543 1731 1855 2205 2092 1568 833 579 1248 1778 2020 1761 1161 1111 1118 969 1150 1089 1016 1312 1629 1539 1319 1054 1186 1488 1232 1303 1760 1638 1945 1589 929 706 968 1400 1347 1672 1427 1238 1360 1495 1250 1235 1656 1539 1167 2157 2489 1740 2854 3334 2490 1480 1498 1913 3093 4006 3508 3057 1748 2028 2403 1846 2383 2356 2731 1870 2512 2295 1856 4149 3909 1665 925 2499 2826 5864 8529 5453 2395 1519 3343 3850 4230 3532 2647 3999 1204 8336 10009 5559 5853 5299 4484 4717 8604 10556 7955 5190 9578 13646 7503 13974 25707 23688 14465 11280 12387 12311 12216 11034 11915 7275 3704 5330 5519 4311 3616 8547 8021 3688 2059 2357 2760 5027 3839 3764 2975 2811 2787 1960 3422 2507 2175 2023 1608 1589 2240 3082 3833 2195 1061 2646 2384 2361 2619 2183 2886 1998 2081 2512 2095 1314 1439 888 1442 1647 1862 1170 1242 2251 2411 1799 1672 1222 1678 1387 1292 1575 1197 977 1201 1330 835 1177 1403 1759 1938 1376 1008 1275 1705 1472 1126 1725 2196 2187 1485 1332 803 994 884 623 1093 1741 1942 1547 1487 1180 1682 1986 1689 1908 1894 1211 1124 1836 2192 2833 3411 3351 3351 2707 2685 2336 2278 1821 1781 1426 1453 1486 1231 1059 1424 1570 1571 1628 1699 1795 1589 1248 1707 2154 2342 2322 2103 1783 1533 1465 1039 1451 1665 2338 2276 3257 3318 2985 808 658 704 1089 1259 990 1612 1428 3585 4636 5277 5353 6152 6280 6467 10488 10840 10361 7610 7353 7314 7972 9080 10024 10627 10784 10353 9473 11271 12860 13800 14049 13689 12666 11257 7717 7888 7728 7247 6512 6717 6734 7523 8074 8318 8208 7736 7029 6144 5893 5612 6345 6755 6914 6780 6371 5704 4442 4088 4165 4732 5414 6067 6573 6871 7008 6950 6795 6846 6731 6446 6035 5511 4913 4790 4765 4641 6015 6389 6590 6648 6552 6289 5916 5433 4935 4361 3754 3190 3283 3724 3800 3890 4233 4538 4775 4964 5054 5076 5010 4905 4717 4469 4181 3886 3562 3839 4096 4292 4463 4548 4582 4564 4488 4370 4200 3979 3729 3446 3133 2801 2506 2378 2429 2480 2486 3783 3858 3914 3929 3931 3926 3890 3843 3767 3692 3622 3572 3512 3430 3336 3251 3132 2989 2867 2712 2575 2427 2286 2118 1977 1833 1687 1554 1857 1784 1798 1782 2221 2207 2187 2138 2097 2042 2006 1949 1875 1816 1767 1697 1633 1565 2653 2625 2596 2549 2482 2423 2332 2236 2150 2052 1940 1800 1673 1554 1431 1306 1190 1123 1119 1106 1095 1074
    3 3 3 3 3 3 3 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
    0.02 0.02 0.02 0.02 0.02 0.02 0.01 0.02 0.02 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.00 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.02 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.00 0.00 0.00 0.00 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.00 0.00 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.01 0.01 0.01 0.02 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.02 0.02 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.01 0.01 0.02 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.01 0.02 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02
    GPS 0 30776.599 73.41 3.13 1.3 2 5
    POS 0 30776.599 760129.14 2314059.18
    GYR 0 30776.599 73.41
    TID 0 30776.599 0.000
    HCP 1 30776.516 -0.03 -5.63 6.82
    HCP 1 30776.532 -0.02 -5.60 6.84
    EC1 2 30776.557 3 0 5b01 512 1544.52 11426
    13.10 12.68 12.57 12.71 12.62 12.58 11.88 12.64 12.54 11.72 11.67 11.62 11.56 11.51 11.46 11.41 11.36 11.31 11.26 11.21 11.16 11.10 11.06 11.01 10.96 10.90 13.10 10.81 10.77 10.72 10.67 10.63 10.57 10.52 10.48 10.42 10.38 10.34 10.29 10.25 10.20 10.15 10.11 10.06 10.01 9.96 9.91 9.87 9.83 9.79 9.75 9.71 9.66 9.63 9.58 9.54 9.49 9.44 9.40 9.36 9.32 9.29 9.25 9.21 9.17 9.12 9.08 9.04 9.01 8.97 8.94 8.89 8.85 8.81 8.78 8.74 8.71 8.67 8.63 8.59 8.55 8.51 8.47 8.44 8.40 8.37 8.33 8.29 8.26 8.23 8.18 8.15 8.11 8.08 8.04 8.01 7.98 7.95 7.91 7.89 7.86 7.84 7.83 7.80 7.74 7.71 7.70 7.67 7.63 7.61 7.59 7.56 7.53 7.50 7.49 7.45 7.43 7.39 7.39 7.36 7.34 7.32 7.29 7.27 7.25 7.22 7.18 7.16 7.16 7.15 7.13 7.11 7.09
    -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000 -90.000

    Thanks and Regards

    Sunil

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: Read Text file and make changes to text by searching a string

    so all the values in red should be changed to 0.5 (or whatever value is determined as the replacement value)?
    except the actual EC1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,999

    Re: Read Text file and make changes to text by searching a string

    Sounds like you just need to add a loop for the replacement.
    Read in each line then split then in a loop check to see if the column value=-90 if not then replace if yes then exit the loop and continue on.

    The check for -90 on each column would not be needed if the -90 is always going to be in the first column of the next line. If that is the case then you would just check for it in the first column and if not present then process the entire line in your loop then continue to the next line.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Hi
    Changes in values are to be made in line below the string EC1.
    Now i wonder what command / code to use for selection of every string?. The code included below selects the 3rd string , as earlier the data contained was of a single value change at string 3.

    Can u guide/ suggest please

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Yes
    All the values in red are to be changed except the string EC1 in the text file.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Sir
    as u suggested that
    "Read in each line
    then split
    then in a loop check to see if the column value=-90"

    how /what code shall i use for read each line below string/ after string EC1
    splitting into string i did in the previous codes used (will that be a same here as the line might contain many strings)
    then every string in that line is to be modified by a value (from text box or for now + 0.5)
    Now here how do i select every string (code/ command for selection of every string in line)
    then the loop for check to see if the column value=-90 can be added
    then finally reassemble all the text/ values modified and print to file.

    Text file containing data to be changed is at below link for reference or test?check

    https://www78.zippyshare.com/v/RZLcUpP9/file.html


    please help
    Last edited by skr706; Jun 19th, 2019 at 01:21 AM.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,999

    Re: Read Text file and make changes to text by searching a string

    I did not fully examine your code that is posted but if you wrote that code then you should be able to create the required loop.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: Read Text file and make changes to text by searching a string

    Sounds like you just need to add a loop for the replacement.
    Read in each line then split then in a loop check to see if the column value=-90 if not then replace if yes then exit the loop and continue on.

    The check for -90 on each column would not be needed if the -90 is always going to be in the first column of the next line. If that is the case then you would just check for it in the first column and if not present then process the entire line in your loop then continue to the next line.
    initially i started with as slurp and split, but with the file size i changed to use line input
    Code:
    Dim ec As Boolean, i As Integer
    Dim strln As String, avals() As String
    repval As String, fn As String, fn2 As String
    fn = "c:\temp\0001_0758.txt"
    fn2 = "c:\temp\0001_0758X.txt"
    repval = "0.5"
    Open fn For Binary As 1
    Open fn2 For Output As 2
    ' write to another file so can compare changes
    Do
    Line Input #1, strln
        If Left(strln, 7) = "-90.000" Then
            ec = False
            Else
            If ec Then   ' edit the value in the lines
                avals = Split(strln)
                If UBound(avals) > 0 Then
                    For i = 0 To UBound(avals)
                        avals(i) = repval
                    Next
                    strln = Join(avals)
                End If
            End If
        End If
        If Left(strln, 3) = "EC1" Then ec = True ' in this position the line will not be edited, but future lines will be edited while ec = true
        Print #2, strln  ' write out edited or original line to new file
    Loop Until EOF(1)
    
    
    Close 1
    Close 2
    ' when testing complete, if you want the file to have the name of the original, kill the original and name the copy to original
    change all paths and replacement value to suit
    rename any variables to suit yourself
    do extensive testing to check the results are all correct
    the results appeared to be correct, but it was not a thorough comparison
    the code took about 23 seconds on my old laptop, with the sample file, so i guess some room for improvement

    better to always post sample files etc in the forum rather than some outside host
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Hi
    Thanks for your guidance and code.
    It worked, but i made a minor change as required.

    Code:
    Private Sub Command1_Click()
    Dim ec As Boolean, i As Integer
    Dim strln, repval, fn, fn2, avals() As String ' avals() As double
    'repval As String, fn As String, fn2 As String
    pi = 0.5
    fn = "c:\temp\0001_0758.txt"
    fn2 = "c:\temp\0001_0758X.txt"
    'repval = 1
    Open fn For Binary As 1
    Open fn2 For Output As 2
    ' write to another file so can compare changes
    Do
    Line Input #1, strln
        If Left(strln, 7) = "-90.000" Then
            ec = False
            Else
            If ec Then   ' edit the value in the lines
                avals = Split(strln)
                If UBound(avals) > 0 Then
                    For i = 0 To UBound(avals)
                    
                    
                     If IsNumeric(pi) Then
                        avals(i) = avals(i) + Val(pi)
                       
                    Else
                        avals(i) = avals(i) + 0#
                    End If
                    
                    
                    
                    Next
                    strln = Join(avals)
                End If
            End If
        End If
        If Left(strln, 3) = "EC1" Then ec = True ' in this position the line will not be edited, but future lines will be edited while ec = true
        Print #2, strln  ' write out edited or original line to new file
    Loop Until EOF(1)
    
    
    Close 1
    Close 2
    ' when testing complete, if you want the file to have the name of the original, kill the original and name the copy to original
    End Sub
    here this hangs the sequence and ends up with debug window and stops at first line as yellow
    "Line Input #1, strln"
    any idea why it does so
    does it require any error handing code too. or everytime it will hang up here.
    i shall try to figure out whats wrong? If you catch up then please do let me know.

    Thanks a lot for your guidance.
    your support is highly appreciated.
    Sunil

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: Read Text file and make changes to text by searching a string

    are you testing against the same sample file? what error are you getting?
    i had no error when i tested

    is the error happening at the beginning or end of the file
    you could add a counter to be able to find when the error occurs
    Code:
    Do
    cnt = cnt + 1
    Line Input #1, strln
    you would then be able to test which line in the file is causing the error and can check if there is a problem in the file

    does it require any error handing code too. or everytime it will hang up here.
    error handling is always good, but you need to know what error you might get to know how to handle, in which case it miight be easier to resolve the problem

    either way the issue is fixable, just a matter of finding what to fix

    Dim strln, repval, fn, fn2, avals() As String '
    in this case srln fn and fn2 are all dimensioned as variants, not that it will cause your error

    avals(i) = avals(i) + 0#
    adding a double to a string and returning a string may also cause a problem with formatting and will require 2 implicit type conversions
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Hi
    thanks once again for your reply.
    1. I hanged the avals as string
    2.I had noticed that the input file had a string before -90.00 at few places so the code stopped incomplete
    0.000 0.000 0.000 -90.000
    manually i deleted the strings in input file.
    3. can we add a if statement
    Code:
    If Left(strln, 5) = "0.000" Then
        ec = False
    but here the possibilities are there that if any value (false) is in between then the code will leave the remaining values addition until next EC1

    code runs until end of file.
    at the end it gives an error of "Input past end of file"
    Name:  Untitled.jpg
Views: 73
Size:  18.2 KB

    4. after test how can i make the same file as input file instead of deleting file2 or renaming. (input file, read, edit, save same file)

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: Read Text file and make changes to text by searching a string

    4. after test how can i make the same file as input file instead of deleting file2 or renaming. (input file, read, edit, save same file)
    easy to do with code, but until testing is complete, need to keep old file
    Code:
    kill fn
    name fn2 as fn

    2.I had noticed that the input file had a string before -90.00 at few places so the code stopped incomplete
    do you want to edit the values before the -90.000?
    or if -90.000 is in the line, not to be edited at all
    to detect if the value is anywhere in the line change to
    Code:
    Line Input #1, strln
        If Instr(strln, "-90.000" > 0 Then

    it gives an error of "Input past end of file"
    try changing to
    Code:
    Open fn For Input As 1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Hi
    something is still not going good.

    pi=1
    If IsNumeric(pi) Then
    avals(i) = avals(i) + Val(pi)

    Else
    avals(i) = avals(i) + 0#
    End If
    here pi is a value (integer) and avals(i) is a string
    i think string + integer is not a good sequence or statement, thats the reason its highlighting the codes here.
    Although it does its work and print the output file but seems as if not stopping the process due to this error.
    Any solution to this please?

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: Read Text file and make changes to text by searching a string

    as far as i can tell this should not cause any error though the formatting of the output file may not be as desired
    you many need to do like
    Code:
    avals(i) = format(avals(i) +pi,"##.000")
    formatting will convert the calculation to a string

    is there some point to adding 0 to the original amount? if so you may also need to format that output

    are you testing against the same sample file?
    from post #10,
    if not post the sample file you are working with now and the full code you are using, so we can see any changes that may cause an error

    i got no errors when testing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    51

    Re: Read Text file and make changes to text by searching a string

    Hi
    Thanks very much

    Resolved

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width