Results 1 to 22 of 22

Thread: [RESOLVED] Read Text file and make changes

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Resolved [RESOLVED] Read Text file and make changes

    I am in need of programming in VB on a simple project.

    Few files r enclosed along the mail

    I tried to make the program but am not able to proceed ahead
    Please check me file.
    What u need to do
    Create a folder c:\sunil
    Copy the test.txt file there
    Use the frm to make exe, select the file test click at the bottom on check box
    Press convert, the result file will also go to c”\sunil.

    Here the test .txt file has only 4 cloumns and I just needed to add 0.5 to the last value--------------easy

    What I need

    I need to use the same type by reading a file as “000_1642.RAW”
    Program should Read the whole file
    Wherever it finds EC1 then in that line in the last figure it should add 0.5 (text2.text)
    Rest of the file should remain same

    Like below

    FTP NEW 2
    VER 6.2.2.8
    INF "" "" "" "" 0.000 0.000 0.000
    FIL "" "C:\HYPACK\Projects\JAIGAD\DREDGING CHANNEL.lnw"
    ELL WGS-84 6378137.000 298.257223563
    PRO TME xxxxx 75.000000 0.999600 0.000000 0.000000 0.000000 500000.0000 0.0000
    DTM 0.00 0.00 0.00 0.00000 0.00000 0.00000 0.00000
    GEO ??? 0.000
    HVU 1.0000000000 1.0000000000
    TND 16:42:00 11/10/2011 -330
    DEV 0 32868 "Nmea 0183" 49156 C:\HYPACK\devices\nmea.dll 6.2.7.25l
    OFF 0 0.000 0.000 0.000 0.000 0.000 0.000 0.000
    DEV 1 16 "Echotrack GLDD" 512 C:\HYPACK\devices\echotrackgldd.dll 6.2.1.3
    OFF 1 0.000 0.000 0.000 0.000 0.000 0.000 0.000
    EOH
    EC1 1 60118.328 5.280------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60118.497 5.370------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60118.666 5.310------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60118.835 5.310------------------------------ this value should be added by 0.5 or whatever given in text2.text
    POS 0 60118.811 310607.431 1915392.695
    QUA 0 60118.811 4 9.100 0.900 9.000 2.000
    RAW 0 60118.811 4 171896.15000 731307.65000 -74.40000 111206.00000
    EC1 1 60119.005 5.340
    GYR 0 60118.974 91.000
    EC1 1 60119.174 5.340------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60119.344 5.400------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60119.513 5.400------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60119.682 5.370------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60119.851 5.370------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60120.021 5.400------------------------------ this value should be added by 0.5 or whatever given in text2.text
    POS 0 60119.867 310606.888 1915391.409
    QUA 0 60119.867 4 9.100 0.900 9.000 2.000
    RAW 0 60119.867 4 171896.08000 731307.62000 -74.40000 111207.00000
    EC1 1 60120.190 5.400------------------------------ this value should be added by 0.5 or whatever given in text2.text
    EC1 1 60120.359 5.400------------------------------ this value should be added by 0.5 or whatever given in text2.text
    FIX 99 60120.500 739

    Till end of file and save the file in c:\sunil by the same name


    Can you please help me by making it possible
    Thanks in advance
    mail me on Email Address Removed By Mod
    Attached Files Attached Files

  2. #2
    Addicted Member Darren M.'s Avatar
    Join Date
    Nov 2005
    Location
    D/FW
    Posts
    200

    Re: Read Text file and make changes

    So, you just want to open the file, parse the 4th column on lines that start with EC1 and increment the value by another value stored in a Text box and the write the results out to the new file?

    My attempt at a thought provoking sig.
    * Remove ALL assumptions. You'll probably find your problem.
    * New to the forums? READ THIS! The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft
    * Learn how to fish!
    * Subscribing to every thread I post in is like having a book mark in my mind. Every now and then I get to review a fond memory.
    * If you find my post helpful do me a 15 second favor and RATE it. (Rate Post. Over there <----)
    * Please mark all resolved threads as such. It makes searching for fixes SO much easier!
    * Have fun. We aren't on this rock for long, enjoy it.
    * Sometimes I'm wrong. When I am I admit it. Never am I malicious. If reading my post and you think otherwise, then it MAY be tongue in cheek. Read it again.

  3. #3
    Addicted Member Darren M.'s Avatar
    Join Date
    Nov 2005
    Location
    D/FW
    Posts
    200

    Re: Read Text file and make changes

    I have a feeling we may never from our new friend Sunil again...... However, for some reason this caught my attention. Here's the way I would accomplish this. I'm only on my first cup of coffee this morning. So, I'm sure there is a more elegant way of accomplishing the same results. Anyone else want to take a crack at it, or suggest a better way of doing the same?

    (You'll need to create a simple 1 button, 1 text box form.)
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Const strTARGET_LINE    As String = "EC1"
    5.     Const strFILE_DELIMITER As String = " "
    6.     Const lngTARGET_ELEMENT As Long = 3
    7.    
    8.     Dim intFreeFile As Integer
    9.    
    10.     Dim strDelimiter    As String
    11.     Dim strCrLf         As String
    12.     Dim strPath         As String
    13.     Dim strErrMsg       As String
    14.     Dim strFileData     As String
    15.     Dim strInputLine    As String
    16.     Dim strLineData()   As String
    17.    
    18.     Dim varInputLine    As Variant
    19.  
    20.     Dim lngCounter      As Long
    21.    
    22. On Error GoTo ERRORHANDLER
    23.    
    24.     If Not IsNumeric(Me.Text1.Text) Then
    25.         strErrMsg = "You must enter a value > 0 in the Text box"
    26.         GoTo ERRORHANDLER
    27.     End If
    28.  
    29.     ' Change to reflect your path or read from text box or other logic
    30.     strPath = App.Path & "\Sunil.txt"
    31.    
    32.     intFreeFile = FreeFile()
    33.    
    34.     Open strPath For Input As intFreeFile
    35.    
    36.         Do While Not EOF(intFreeFile)
    37.        
    38.             Line Input #intFreeFile, varInputLine
    39.            
    40.             strInputLine = Trim$(CStr(varInputLine))
    41.            
    42.             If Len(strInputLine) > 3 Then
    43.            
    44.                 If Left(strInputLine, Len(strTARGET_LINE)) = strTARGET_LINE Then
    45.                
    46.                     strLineData() = Split(strInputLine, " ", -1, vbBinaryCompare)
    47.                    
    48.                     If UBound(strLineData) >= lngTARGET_ELEMENT Then
    49.                         strInputLine = ""
    50.                        
    51.                         For lngCounter = LBound(strLineData) To UBound(strLineData)
    52.                        
    53.                             If lngCounter = lngTARGET_ELEMENT Then
    54.                            
    55.                                 ' Add Error handling to ensure you are incrementing a number
    56.                                 strLineData(lngCounter) = Format(CStr((CSng(strLineData(lngCounter) + CSng(Me.Text1.Text)))), "####0.000")
    57.                                 strDelimiter = strFILE_DELIMITER
    58.                             End If
    59.                            
    60.                             strInputLine = strInputLine & strDelimiter & strLineData(lngCounter)
    61.                             strDelimiter = strFILE_DELIMITER
    62.                         Next lngCounter
    63.                        
    64.                         strDelimiter = ""
    65.                     End If
    66.                 End If
    67.             End If
    68.            
    69.             strFileData = strFileData & strCrLf & strInputLine
    70.             strInputLine = ""
    71.             strCrLf = vbCrLf
    72.         Loop
    73.    
    74.     Close intFreeFile
    75.    
    76.    
    77.     Kill strPath
    78.    
    79.     intFreeFile = FreeFile
    80.     Open strPath For Output As intFreeFile
    81.     Print #intFreeFile, strFileData
    82.     Close intFreeFile
    83.    
    84.  
    85. GoTo OVERERROR
    86.  
    87. ERRORHANDLER:
    88.     If LenB(strErrMsg) = 0 Then
    89.         strErrMsg = Err.Number & ": " & Err.Description
    90.     End If
    91.    
    92.     MsgBox strErrMsg, vbCritical, "Error"
    93.    
    94. OVERERROR:
    95.    
    96. End Sub

    My attempt at a thought provoking sig.
    * Remove ALL assumptions. You'll probably find your problem.
    * New to the forums? READ THIS! The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft
    * Learn how to fish!
    * Subscribing to every thread I post in is like having a book mark in my mind. Every now and then I get to review a fond memory.
    * If you find my post helpful do me a 15 second favor and RATE it. (Rate Post. Over there <----)
    * Please mark all resolved threads as such. It makes searching for fixes SO much easier!
    * Have fun. We aren't on this rock for long, enjoy it.
    * Sometimes I'm wrong. When I am I admit it. Never am I malicious. If reading my post and you think otherwise, then it MAY be tongue in cheek. Read it again.

  4. #4

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Darren,
    Thanks for the prompt reply,
    While adding the path to the file (c:\sunil.txt) I get a error message of 52:bad filename or path
    Could u please make the frm and send
    Best Regards
    Sunil

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Darren,

    I am a beginner in VB6 and need lots to learn more,
    I thought this tool could be very simple, but it doesnt looks like.
    Could u please add the path of the file as c:\sunil.txt as a primary path, later I shall change as required. or can u please add a browser to the frm?help shall be highly appreciated in this regard
    Sunil

    Ur

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Darren,

    I tried to add something like this:
    ' Change to reflect your path or read from text box or other logic
    'strPath = App.Path & "\Sunil.txt"



    strPath = App.Path
    If Right(strPath, 1) <> "\" Then
    strPath = strPath & "c:\Sunil.txt"
    Else
    strPath = strPath & "Sunil.txt"
    End If


    Didnt work out.

    Tried few more options but still file /path error.

    Could u please solve it by adding a browser and giving the path to file? and make the frm?

    ** what if I need to convert multiple files at a time?

    Best Regards
    Sunil

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Darren,

    Got it!!!!!!!
    Thanks
    I copied the file in the working folder and it worked.

    ** Now comes for browsing the file,
    1. How can the file be browsed with a strpath = App.Path & "filename or path" could u add a browser?
    2. I need to select multiple files at 1 go.
    3. Message on completion of task, Like "Done" so it confirms that its done else just keep on clicking!!

    Thanks once again and still waiting for your reply
    Sunil

  9. #9
    Addicted Member Darren M.'s Avatar
    Join Date
    Nov 2005
    Location
    D/FW
    Posts
    200

    Re: Read Text file and make changes

    Sunil, please keep in mind that we could very well be on completely different sides of the world. I would speculate that you are probably 9 to 12 hours ahead of me in time. (I live in Texas. -6 gmt) Also, the things you are requesting are literally spread through out this forum. You'll need to spend a bit of time searching for the pieces you need to use to put your application together. I feel I may have failed you by supplying so much code all at once and not asking that you write some of it first. A mistake I won't make again. Please use the search feature in the forum and look for things you need. Spend some time in the developer and post up what you have. I will help you from there but I will not write the application for you.

    My attempt at a thought provoking sig.
    * Remove ALL assumptions. You'll probably find your problem.
    * New to the forums? READ THIS! The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft
    * Learn how to fish!
    * Subscribing to every thread I post in is like having a book mark in my mind. Every now and then I get to review a fond memory.
    * If you find my post helpful do me a 15 second favor and RATE it. (Rate Post. Over there <----)
    * Please mark all resolved threads as such. It makes searching for fixes SO much easier!
    * Have fun. We aren't on this rock for long, enjoy it.
    * Sometimes I'm wrong. When I am I admit it. Never am I malicious. If reading my post and you think otherwise, then it MAY be tongue in cheek. Read it again.

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Darren,
    Thanks for your help u did.

    Only I needed is- I am not able to link the path with the browser or textbox
    strpath=app path is not working there, what command shall be used there?
    Could u suggest how to link it? only when u r free and having a cup of coffee!!!

    Best Regards
    Sunil

  11. #11
    Addicted Member Darren M.'s Avatar
    Join Date
    Nov 2005
    Location
    D/FW
    Posts
    200

    Re: Read Text file and make changes

    Sunil, I want to help you, I really do. However, there is an old saying from a book I refer to from time to time. I paraphrase:
    If you feed a man a fish, he eats for a day. However, if you can teach the man to fish, he can eat for a lifetime.
    My attempt here, is to teach you how to fish. I hope you take this as a helpful lesson on how to find the things you are looking for in this Forum.
    Goto this link and bookmark it.
    http://www.vbforums.com/search.php

    Scroll down and select "Visual Basic FAQ" (Frequently Asked Questions). Then enter some key words to search by. Here's a quick example of the exact question you have "How can I show an open file dialog" the results will show up and you can review the threads. The better your search words the better the results. This particular thread, Index to FAQ's: Classic VB (VB6 and earlier), stands out because it is a "STICKY", or rather it is always at the top of the forum, it "sticks". If you were to review that thread you'd see an index that includes Files. By reading that you'll find the EXACT answer to your question.
    You see, we don't mind helping people. But we do wish that people would read the material already in place. That's why we have the http://www.vbforums.com/search.php function, to use material already posted.

    Now, when you get finished with this project, if you would like to contribute back to the forum for people in the future who, hopefully, use the SEARCH. You can post your code, no EXE's or DLL's please, to this thread. And/or post the code using the forum tags of [highlight=vb] Your code goes here [/highlight]. This will result in your code looking like the following.

    vb Code:
    1. 'Your code goes here
    2. Dim strThisIsaSample as string

    My attempt at a thought provoking sig.
    * Remove ALL assumptions. You'll probably find your problem.
    * New to the forums? READ THIS! The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft
    * Learn how to fish!
    * Subscribing to every thread I post in is like having a book mark in my mind. Every now and then I get to review a fond memory.
    * If you find my post helpful do me a 15 second favor and RATE it. (Rate Post. Over there <----)
    * Please mark all resolved threads as such. It makes searching for fixes SO much easier!
    * Have fun. We aren't on this rock for long, enjoy it.
    * Sometimes I'm wrong. When I am I admit it. Never am I malicious. If reading my post and you think otherwise, then it MAY be tongue in cheek. Read it again.

  12. #12

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Darren,

    After fishing for these many days, I couldnt get success.
    Tried many codes, downloaded many sample codes and tried to insert to get success, but unfortunate.

    I request once again if u can help me out with the right fish in form of the code for selection of the
    1. file from browser.
    2. Selection of multiple file of same extension and apply the code to all files.

    Thanks and best regards
    Sunil

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Read Text file and make changes

    The example data does not lend itself to easy analysis as there could be the odd space around that could cause problems.

    If you look at the file and your requirements you need to :
    1. Identify lines beginning with EC1
    2. When you find one look for the 3rd value (where a 'value' is defined as something following a space character)
    3. Add to the 3rd value whatever is in Text2.Text

    A method might be:
    Code:
    Option Explicit
    
    Private Sub cmdUpdate_Click()
    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 strRecords() As String
    Dim strFields() As String
    '
    ' Allocate a File Number, open, read the entire contents and close the file
    '
    intFileIn = FreeFile
    Open "C:\sunil\000_1642.RAW" For Input As intFileIn
    strData = Input(LOF(intFileIn), intFileIn)
    Close intFileIn
    '
    ' Open the output file
    '
    intFileOut = FreeFile
    Open "C:\sunil\000_1642_out.RAW" 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))
                    dblVal = dblVal + CDbl(Text2.Text)
                    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
    C:\sunil\000_1642_out.RAW will be a copy of the input file with the updated values

    If you need to process multiple files then research the CommonDialog control.
    Last edited by Doogle; Jan 18th, 2012 at 01:15 PM.

  14. #14

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Doogle,

    Thanks for your guidance,
    I shall also keep trying for the multiple option search and as suggested research the CommonDialog control.

    Thanks once again.
    Sunil

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Doogle,

    I found a code for selecting multiple files. as mentioned below, But here I am unable to link the files with the previous code to operate. Could U please help me in this regard,

    '_________Code__________

    Dim strFileNames As Variant
    Dim i As Integer
    strFileNames = Split(GetFiles, Chr(0))
    With List1
    If UBound(strFileNames) > 0 Then
    .Clear
    'Path is stored in index 0 of array files in index 1...
    Label1.Caption = "Files selected from: " & strFileNames(0)
    For i = 1 To UBound(strFileNames)
    .AddItem strFileNames(i)
    Next
    Else
    .AddItem "(No files selected)"
    End If
    End With
    End Sub

    Public Function GetFiles(Optional ByVal sTitle As String = "Open files...") As String
    ' sTitle: Optional Title of Dialog
    Dim sFilenames As String
    Dim cdlOpen As Object
    On Error GoTo ProcError
    ' Get the desired name using the common dialog
    Set cdlOpen = CreateObject("MSComDlg.CommonDialog")
    ' set up the file open dialog file types
    With cdlOpen
    ' setting CancelError means the control will
    ' raise an error if the user clicks Cancel
    .CancelError = True
    .Filter = "Raw Files (*.raw)|*.raw|All Files (*.*)|*.*"
    .FilterIndex = 1
    .DialogTitle = sTitle
    .MaxFileSize = &H7FFF ' 32KB filename buffer
    ' 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
    ProcExit:
    GetFiles = sFilenames
    Set cdlOpen = Nothing
    Exit Function
    ProcError:
    If Err.Number = &H7FF3 Then Resume Next 'Cancel selected - Ignore
    MsgBox Err.Description & "(" & Err.Number & ")", vbExclamation, "Open error"
    sFilenames = ""
    Resume ProcExit

    End Function
    '___End code_________

  16. #16
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Read Text file and make changes

    There's a couple of things to note with the code you just posted:

    (1) If only 1 file is selected, element 0 contains the full path and file
    (2) There's no need to trap an error if the user doesn't select a file. The UBound is set to -1 in that case.

    I have adjusted your code (and mine in Post #13) and integrated them. There are now two CommandButtons, one to Create the list of files (cmdGetFileNames) which will populate the ListBox and the other (cmdCreateOutput) which will perform the processing on each file in the LstBox (it removes the file from the ListBox when it's been processed)

    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
    Note that it will add whatever is in Text2.Text (or 0.5) to the EC1 value for every file.

  17. #17

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Doogle,

    Thanks for the kind help.
    ===Resolved====

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Read Text file and make changes

    Thanks for letting it know it is resolved, but the way you have done it isn't clear until time has been spent reading the entire thread.

    The "proper" way to do it (which alters the thread title etc, thus making it immediately obvious) is to click on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

  19. #19

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Dear Doogle,
    Although this post was resolved, I thought of adding some more codes to it with your kind help.

    The code you helped works fine, Now I planned to add some more code to it for getting the date created/date accessed/ date modified for the file selected.

    My requirement is :
    while the previous code reads the file (while multiple files are selected)
    it should read the file name and get the date created/accessed/modified
    then run the process
    then apply the date created/accessed/modified
    then --nextI

    I want to insert the code:

    Private Sub File1_list()
    'use the Nessalco DateStamp Control to report a file's DateCreated,
    ' to get the selected files date created/accessed/modified
    List1.Clear
    DateStamp1.File = "File name required which is stored in label1.caption and intI"
    txtCreated.Text = DateStamp1.dCreated
    txtModified.Text = DateStamp1.dModified
    txtAccessed.Text = DateStamp1.dAccessed
    End Sub

    'After the string process is over and before selection of next file need to insert the code:

    Date = DateStamp1.StampFile(LoadPath & File1.FileName, txtCreated.Text, txtModified.Text, txtAccessed.Text)

    then continue the procedure till the end of file.

    I did it in the procedure while selecting single file but unable to insert while selecting multiple files.
    Could you please help it out in inserting it.

    Best Regards
    Sunil
    Last edited by skr706; Jun 16th, 2012 at 11:22 PM.

  20. #20

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    58

    Re: Read Text file and make changes

    Hi Mr. Doogle
    I need your expert help once again as im unable to add a simple calculation in the same file and same codes.

    The codes are as below .
    Code:
    Code:
    .  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) & Mid$(strFile, intPos)
    Else
        strOFile = strFile
    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 EC2 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), "EC2")
            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
    Next intI
    Close intFileOut
    End Sub
    The code runs the routine for changing the value of string 3 at the text EC2 and + or - the value of text1.text.

    My new requirement here is that i need to copy the string 4 value at the string value 3

    MSG 1 51186.023 $GPZDA,084306.023,21,11,2021,00,00*5C
    MSG 1 51186.070 $GPHDT,,*4F
    MSG 1 51186.083 $GPGLL,2104.97647917,N,08845.79220052,E,084306.00,A,D*6E
    HCP 2 51186.193 0.170 -0.670 1.430
    HCP 2 51186.293 0.150 -0.810 1.490
    EC2 0 51186.404 11.200 16.430
    HCP 2 51186.393 0.140 -0.960 1.550
    HCP 2 51186.493 0.120 -1.130 1.610
    HCP 2 51186.593 0.100 -1.300 1.690
    HCP 2 51186.693 0.080 -1.480 1.760
    HCP 2 51186.793 0.070 -1.670 1.840
    HCP 2 51186.893 0.050 -1.840 1.910
    HCP 2 51186.993 0.030 -2.010 1.990
    POS 1 51187.000 683164.505 2332341.369
    QUA 1 51187.000 7 3.000 1.000 8.000 2.000 0.000 0.000 0.000

    Here in above data file string 3 is 11.2 and string 4 is 16.43. same way in full file in need to replace the value of string 4 to string 3 and a option of + or - thr value of text 1.text.

    Second problem i have is of large data file errror.

    I used the below code for file size

    Code:
    Code:
     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
        .MaxFileSize = 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
        .MaxFileSize = 12048  ' Add this to correct the behavior for increasing the no of files selection (max file size error).
        .ShowOpen
        ' get the selected name
        sFilenames = .FileName
    End With
    GetFiles = sFilenames
    Set cdlOpen = Nothing
    End Function
      ' Add this to correct the behavior for increasing the no of files selection (max file size error).
        .ShowOpen
        ' get the selected name
        sFilenames = .FileName
    End With
    GetFiles = sFilenames
    Set cdlOpen = Nothing
    End Function
    I also changed the file size to 999999 but not accepted

    Please guide to increase the filesize of larger file. If the data file ismore than 2 mb error shows. Then the full file deletes the existing data.

    The data file is enclosed for usagehttps://mega.nz/file/kigw1C6I#gJYjPX...A75poX7nS-I90E

    Please help to make it work

    Thanks
    Sunil
    skr706 AT gmail.com
    Last edited by Shaggy Hiker; Nov 25th, 2021 at 11:45 AM. Reason: Obfuscated email.

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Read Text file and make changes

    i am not sure that i have seen doogle here for some time

    this last post is the same as in your other thread, as both threads are from more than 2 years ago, you should have started a new thread now with links to your old threads

    i have supplied some answers in your other thread
    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

  22. #22
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Read Text file and make changes

    I do think that a new thread would have been the right way to go. The goal is to get more eyes on the problem, and starting a new thread for this question would do that better. However, there certainly should be only one thread per topic, so I have closed this old thread, so that all the conversation can be in just the one thread, which is here:

    https://www.vbforums.com/showthread....t=#post5545123

    If you would prefer to have that new question split out into its own thread, just send a PM to one of the mods, or report your own post. Either one will work.

    I also obfuscated your email address. You'll get deluged by bots if you post a real email address to a forum.
    My usual boring signature: Nothing

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