I have a several text files that get updated each month - when they are updated certain lines are missing that I always want adding back to the files - the text I want adding back needs to go into a specific part of the text file(its always the same text that needs adding and same location)

Example of the file -

RAJKOT                  VARK23 05400228 22.314472  70.785750110.9022800423
HONG KONG               VHHH07L12468073 22.310917 113.897967111.1007300022
HONG KONG               VHHH07R12468073 22.296675 113.899445109.3007300027
HONG KONG               VHHH25L12468253 22.307431 113.932822108.9025300027
HONG KONG               VHHH25R12468253 22.321122 113.929639110.9025300022
HONG KONG KAI TAK       VHHX31 10930316 22.305500 114.214500109.9031600015
HONG KONG KAI TAK       VHHX13 10930136 22.321667 114.196667111.9008800015
PINAR DELRIO-LA COLOMA  MULM07 06562075 22.333317 -83.651164000.0007500131
PINAR DELRIO-LA COLOMA  MULM25 06562255 22.338892 -83.632686000.0025500131

The text I want adding each time is the lines

HONG KONG KAI TAK       VHHX31 10930316 22.305500 114.214500109.9031600015
HONG KONG KAI TAK       VHHX13 10930136 22.321667 114.196667111.9008800015

and should always appear beneath the lines HONG KONG (the entries are actually sorted by the lat co-ordintates)

There are some other files that also need similar entries adding the contents of the files do change when they are updated so my addition needs to be just that and not modify any of the other entries.

Can anyone suggest some code I can run each time to make this work?

Thanks in advance

James

Recommended Answers

All 9 Replies

See if this helps.

Imports System.IO
Public Class Form1
    Private myFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\The HONG KONG Test.txt" '// your file.
    Private arX() As String = Nothing '// String Array to real File into.
    Private sFileContent As String = Nothing '// String to rewrite content back to file with.
    Private xnL As String = vbNewLine '// shortcut for vbNewLine. :D
    Private sCity As String = "HONG KONG"
    '// your 2 Strings to add to file if sCity located.
    Private s1 As String = sCity & " KAI TAK       VHHX31 10930316 22.305500 114.214500109.9031600015"
    Private s2 As String = sCity & " KAI TAK       VHHX13 10930136 22.321667 114.196667111.9008800015"
    Private isFileEdited As Boolean '// determine if worth saving or not.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If File.Exists(myFile) Then '// check if file exists.
            arX = File.ReadAllLines(myFile, System.Text.Encoding.Default) '// load file lines into Arrays.
            isFileEdited = False '// set to False and only save if True.
            For i As Integer = 0 To arX.Length - 1 '// loop thru Arrays.
                If Not sFileContent = "" Then sFileContent &= xnL & arX(i) Else sFileContent = arX(i) '// store line content.
                If arX(i).StartsWith(sCity) Then '// check if current line .StartsWith...
                    If Not i = arX.Length - 1 Then '// if not last line of file.
                        '// check if not next line .StartsWith..., Then...
                        If Not arX(i + 1).StartsWith(sCity) Then sFileContent &= xnL & s1 & xnL & s2
                    Else '// add to end of file.
                        sFileContent &= xnL & s1 & xnL & s2
                    End If
                    isFileEdited = True '// content was added.
                End If
            Next
            If isFileEdited Then '// if content was added, Save.
                File.WriteAllText(myFile, sFileContent, System.Text.Encoding.Default) '// save content back to file.
                MsgBox("File has been edited and saved.")
            Else '// if no content added.
                MsgBox("File was not modified.")
            End If
        Else
            MsgBox("File does not exist.")
        End If
    End Sub
End Class

You might also want to check if not the 2 lines following "HONG KONG", are the same lines that you plan to add to the file.

Thankyou - not at my PC at present so will try this later - but thanks for the quick and helpful response - yes think will need to check that the two lines dont already exist.

Do let me know how it goes and if you have any further questions, you know what to do.

note:
I just recently saw this thread that has Encoding and Decoding. I was only aware of Enconding, not Decoding.
If Encoding and Decoding is not needed in my provided code, you should be able to remove those commands from loading/saving the file; although if needed, look more in depth into how those 2 differenciate and how to get them to respond properly.

Ok works - However if the code is run more than once - it duplicates all the contents of the file again? then adds another set of the two lines.

So what I think I need is for it to check if the lines exist if not they are added - if they exist it checks if they exist it checks if they are the same, modifies if needed otherwise makes no change.

Seems reasonable, have fun, and I hope it is not too confusing to pick up on the posted code.:)

Ok thanks for your help - so a simple check to see if arx(i).contains(s1) or (s2) then end for be sufficient to stop it modifying the file?

However say it contains KAI TAK but the co-ordinates are different Id then want it to replace the entry - so how would I do that? (think easiest option is if it finds HONG KONG KAI TAK it deletes that line then adds the s1 and s2 - but not sure how to quite make that happen?

Ok still a work in progress at present - I have managed to add some more features to it - as eventually it will allow user to select which program the data is to be updated in as there are multiple files that may require the change to occur - So i have modified your core code to first determine which program, it then changes the active path(ive used a reg search first to check to see where the user as the app installed) then it modifies to relevant files.

I have, probably somewhat crudely, placed an if statement in to check if the line is the same as either of the to S strings and if so it quits the loop setting it to not modified - which prevents a double modification from being done.

However Im a bit stuck now with finding away to check to see if the line contains KAI TAK but wrong co-ordinates as if it does contain a KAI TAK entry that isnt the same as my S1 and S2 then it needs removing and replacing with my enteries. how do I remove lines?

Please ignore all the msgbox parts they were just for my testing

Imports System.IO
Imports Microsoft.Win32
Public Class Form1
    Dim app As String = ""
    Dim rk As RegistryKey
    Dim PMDGpath As String = ""
    Dim FSBUILDPath As String = ""
    Dim LEVELDPath As String = ""
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Dim sApp As String

    Private Sub PMDG_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PMDG.CheckedChanged
        rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe")
        If rk IsNot Nothing And PMDG.Checked = True Then
            PMDGpath = rk.GetValue("Path")
            sApp = "PMDG"
            MsgBox(PMDGpath)
        ElseIf (PMDGpath) = "" And PMDG.Checked = True Then
            MsgBox("Cant find PMDG")

        End If
        rk.Close()
    End Sub

    Private Sub FSBUILD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FSBUILD.CheckedChanged
        rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\fsbuild2.exe")
        If rk IsNot Nothing And FSBUILD.Checked = True Then
            FSBUILDPath = rk.GetValue("Path")
            sApp = "FSBUILD"
            MsgBox(FSBUILDPath)
            ChDir(FSBUILDPath & "\navdata")
            MsgBox(CurDir)
        ElseIf (FSBUILDPath) = "" And FSBUILD.Checked = True Then
            MsgBox("cant find FSBUILD")
        End If
        rk.Close()
    End Sub

    Private Sub LEVELD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LEVELD.CheckedChanged
        rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\poweriso.exe")
        If rk IsNot Nothing And LEVELD.Checked = True Then
            LEVELDPath = rk.GetValue("Path")
            sApp = "LEVELD"
            rk.Close()

        ElseIf (LEVELDPath) = "" And LEVELD.Checked = True Then
            MsgBox("cant find LevelD")
        End If
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click





        Dim myFile As String = ("test123.txt") '// your file.

        Dim arX() As String = Nothing '// String Array to real File into.
        Dim sFileContent As String = Nothing '// String to rewrite content back to file with.
        Dim xnL As String = vbNewLine '// shortcut for vbNewLine. :D
        Dim sCity As String = "HONG KONG"
        '// your 2 Strings to add to file if sCity located.
        Dim s1 As String = sCity & " KAI TAK       VHHX31 10930316 22.305500 114.214500109.9031600015"
        Dim s2 As String = sCity & " KAI TAK       VHHX13 10930136 22.321667 114.196667111.9008800015"
        Dim isFileEdited As Boolean '// determine if worth saving or not.

        If File.Exists(myFile) Then '// check if file exists.
            arX = File.ReadAllLines(myFile, System.Text.Encoding.Default) '// load file lines into Arrays.
            isFileEdited = False '// set to False and only save if True.
            For i As Integer = 0 To arX.Length - 1 '// loop thru Arrays.
                If Not sFileContent = "" Then sFileContent &= xnL & arX(i) Else sFileContent = arX(i) '// store line content.
                If arX(i) = (s1) Or arX(i) = (s2) Then
                    isFileEdited = False
                    GoTo A
                Else
                    If arX(i).StartsWith(sCity) Then '// check if current line .StartsWith...
                        If Not i = arX.Length - 1 Then '// if not last line of file.
                            '// check if not next line .StartsWith..., Then...
                            If Not arX(i + 1).StartsWith(sCity) Then sFileContent &= xnL & s1 & xnL & s2
                        Else '// add to end of file.
                            sFileContent &= xnL & s1 & xnL & s2
                        End If
                        isFileEdited = True '// content was added.
                    End If
                End If

            Next
A:
            If isFileEdited Then '// if content was added, Save.
                File.WriteAllText(myFile, sFileContent, System.Text.Encoding.Default) '// save content back to file.
                MsgBox("File has been edited and saved.")
            Else '// if no content added.
                MsgBox("File was not modified.")
            End If
        Else
            MsgBox("File does not exist.")
        End If
    End Sub


End Class

Latest Version of code - got rid of nasty GOTO

Imports System.IO
Imports Microsoft.Win32
Public Class Form1
    Dim app As String = Nothing
    Dim rk As RegistryKey
    Dim PMDGpath As String = Nothing
    Dim FSBUILDPath As String = Nothing
    Dim LEVELDPath As String = Nothing
    Dim sApp As String = Nothing
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    '// <<RADIO BUTTON ROUTINES TO SET FILE LOCATION>>

    Private Sub PMDG_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PMDG.CheckedChanged
        rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe")
        If rk IsNot Nothing And PMDG.Checked = True Then
            PMDGpath = rk.GetValue("Path")
            sApp = "PMDG"
            ChDir(PMDGpath & "pmdg\navdata")
            MsgBox(PMDGpath)
        ElseIf (PMDGpath) = "" And PMDG.Checked = True Then
            MsgBox("Cant find PMDG")
        End If
        rk.Close()
    End Sub

    Private Sub FSBUILD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FSBUILD.CheckedChanged
        rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\fsbuild2.exe")
        If rk IsNot Nothing And FSBUILD.Checked = True Then
            FSBUILDPath = rk.GetValue("Path")
            sApp = "FSBUILD"
            MsgBox(FSBUILDPath)
            ChDir(FSBUILDPath & "\navdata")
            MsgBox(CurDir)
        ElseIf (FSBUILDPath) = "" And FSBUILD.Checked = True Then
            MsgBox("cant find FSBUILD")
        End If
        rk.Close()
    End Sub

    Private Sub LEVELD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LEVELD.CheckedChanged
        rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\poweriso.exe")
        If rk IsNot Nothing And LEVELD.Checked = True Then
            LEVELDPath = rk.GetValue("Path")
            sApp = "LEVELD"
            ChDir(LEVELDPath & "LevelD\navdata")
            rk.Close()
        ElseIf (LEVELDPath) = "" And LEVELD.Checked = True Then
            MsgBox("cant find LevelD")
        End If
    End Sub

    '// Update button routine to make changes to selected app

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myFile As String = ""

        If sApp = "FSBUILD" Then
            myFile = "apts.txt"

            '// <<MODIFICATION ROUTINE FOR FSBUILD>>

        ElseIf sApp = "PMDG" Then
            myFile = "wpNavAPT"

            '// <<MODIFICATION ROUTINE FOR PMDG>>

            Dim arX() As String = Nothing '// String Array to real File into.
            Dim sFileContent As String = Nothing '// String to rewrite content back to file with.
            Dim xnL As String = vbNewLine '// shortcut for vbNewLine. :D
            Dim sCity As String = "HONG KONG"
            '// your 2 Strings to add to file if sCity located.
            Dim s1 As String = sCity & " KAI TAK       VHHX31 10930316 22.305500 114.214500109.9031600015"
            Dim s2 As String = sCity & " KAI TAK       VHHX13 10930136 22.321667 114.196667111.9008800015"
            Dim isFileEdited As Boolean '// determine if worth saving or not.
            Dim alreadytedited As Boolean

        If File.Exists(myFile) Then '// check if file exists.
            arX = File.ReadAllLines(myFile, System.Text.Encoding.Default) '// load file lines into Arrays.
            isFileEdited = False '// set to False and only save if True.
                alreadytedited = False
                While alreadytedited = False
                    For i As Integer = 0 To arX.Length - 1 '// loop thru Arrays.
                        If Not sFileContent = "" Then sFileContent &= xnL & arX(i) Else sFileContent = arX(i) '// store line content.
                        If arX(i) = (s1) Or arX(i) = (s2) Then
                            alreadytedited = True
                        Else
                            If arX(i).StartsWith(sCity) Then '// check if current line .StartsWith...
                                If Not i = arX.Length - 1 Then '// if not last line of file.
                                    '// check if not next line .StartsWith..., Then...
                                    If Not arX(i + 1).StartsWith(sCity) Then sFileContent &= xnL & s1 & xnL & s2
                                Else '// add to end of file.
                                    sFileContent &= xnL & s1 & xnL & s2
                                End If
                                isFileEdited = True '// content was added.
                            End If
                        End If

                    Next
                End While

                If isFileEdited Then '// if content was added, Save.
                    File.WriteAllText(myFile, sFileContent, System.Text.Encoding.Default) '// save content back to file.
                    MsgBox("File has been edited and saved.")
                Else '// if no content added.
                    MsgBox("File was not modified.")
                End If
            Else
                MsgBox("File does not exist.")
            End If
            '// add to second file
            '// add to third file
            '// add to fourth file 

        ElseIf sApp = "LEVELD" Then '// Modifcation routine for LEVLD
            myFile = "wpNavAPT"
        End If
    End Sub


End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.