Question Replace Text in Text File

drumminfreak

Member
Joined
Oct 31, 2011
Messages
5
Programming Experience
Beginner
Hi I haven't posted in a while.... and hoping this is the right place to find an answer.

I'm trying to write a Fixture Creator, ive got most of it done (maybe the long way round but it works upto this point) and i'm stuck.
OK.
What i've got!

Text file called "Playersort1" full of data set out like:

Player1
v
Player12
Player2
v
Player11

and so on.

code that looks like this:

Dim Player AsStringDim Test1 AsString
Dim readText AsString
Dim fso, inputFile, outputFile
Dim str AsString
fso = CreateObject("Scripting.FileSystemObject")
inputFile = fso_OpenTextFile(store() & "PlayerSort1.txt")
str = inputFile.ReadAll

'*******************************************************************************************************
For i AsInteger = 1 To 20
If System.IO.File.Exists(Players() & "Player" & i & ".txt") Then
readText = (Players() & "Player" & i & ".txt")
Dim textParser AsNewTextBox textParser.Multiline = False
textParser.Text = My.Computer.FileSystem.ReadAllText(readText)
Player = "Player" & i
readText = (Players() & Player & ".txt") If System.IO.File.Exists(readText) Then
textParser.Multiline = False
textParser.Text = My.Computer.FileSystem.ReadAllText(readText)
Test1 = textParser.Lines(0)
str = Replace(str, Player, Test1)

outputFile = fso.CreateTextFile(store() & "PlayerSort2.txt", True)
outputFile.Write(str)
outputFile.close() inputFile.close()
EndIf
Else
Exit Sub
End If
Next
End Sub
*******************************************************
Now obviously it replaces all the "Player#" in the text file to their actual names.

The output is fine until it gets to double figures (Player10 +)
For example if Player1 is "Fred"

Then Player10, Player11 and Player12 come out like "Fred0", "Fred1" and "Fred2"

If this is making sense to anyone and they know how i can go about fixing it. Please please please tell me....
 
Player = "Player" & i.ToString

Think that's all it needs. The current assignment means that Player is being stored as String+number which shows in the textbox correctly because of the way VB.Net controls are set up but is not a genuine String. When Replace happens, the string part is found and replaced, the number part is not.
 
Player = "Player" & i.ToString

Think that's all it needs. The current assignment means that Player is being stored as String+number which shows in the textbox correctly because of the way VB.Net controls are set up but is not a genuine String. When Replace happens, the string part is found and replaced, the number part is not.

Hey,Dunfiddlin.... thank you for your reply. and it really is much appreciated.

I have tried doing that though and i still get the same result. :( it really is just blagging my head now.

It doesn't have to be replace, its the only way i know (as i haven't been doing vb.net for long)
 
The more I look at this the more confused and tangled it becomes. Why have you got separate files for each player if all they contain is that players name? Why are you using a TextBox.Text value for what is just a simple string? Clearly what is happening is that somewhere Player10 is being read as Player1 but in all the mess I really can't see where or why. I would be tempted to throw the whole thing out and start with the player's names in a single file (in both senses), ie.

Fred
Ginger
Moriarty

Then I would add a ListBox (doesn't have to be visible) and fill it right at the start of the program. (Actually this could be done with a generic List but one step at a time).

VB.NET:
Dim lines() As String = System.IO.File.ReadAllLines("Names.txt")

        For Each line In lines
            ListBoxNames.Items.Add(line)
        Next

Then the replace function is much simpler to follow.


VB.NET:
Dim Fixtures As String = My.Computer.File.System.ReadAllText("PlayerSort1.txt")

For i = 20 to 1 Step -1 [I]'think about why![/I]

[I]'if there is only one reference to every player in the Playersort file then you can omit the Do While loop.
[/I]
Do While Fixtures.Contains("Player" & i.ToString)

'Replace(Source String, Old Substring, New Substring, [Index of Start Position],[No. of occurences])

Fixtures = Replace(Fixtures, "Player" & i.ToString, ListBoxNames.Items(i-1))

Loop

Next
 
Last edited:
The more I look at this the more confused and tangled it becomes. Why have you got separate files for each player if all they contain is that players name? Why are you using a TextBox.Text value for what is just a simple string? Clearly what is happening is that somewhere Player10 is being read as Player1 but in all the mess I really can't see where or why. I would be tempted to throw the whole thing out and start with the player's names in a single file (in both senses), ie.

Fred
Ginger
Moriarty

Then I would add a ListBox (doesn't have to be visible) and fill it right at the start of the program. (Actually this could be done with a generic List but one step at a time).

VB.NET:
Dim lines() As String = System.IO.File.ReadAllLines("Names.txt")

        For Each line In lines
            ListBoxNames.Items.Add(line)
        Next

Then the replace function is much simpler to follow.


VB.NET:
Dim Fixtures As String = My.Computer.File.System.ReadAllText("PlayerSort1.txt")

For i = 20 to 1 Step -1 [I]'think about why![/I]

[I]'if there is only one reference to every player in the Playersort file then you can omit the Do While loop.
[/I]
Do While Fixtures.Contains("Player" & i.ToString)

'Replace(Source String, Old Substring, New Substring, [Index of Start Position],[No. of occurences])

Fixtures = Replace(Fixtures, "Player" & i.ToString, ListBoxNames.Items(i-1))

Loop

Next



The reason is, because it holds more data than just their name, otherwise one text file would have made it simple. I couldn't use access to hold the data, i couldn't be arsed with setting up sql, plus its too much for what i need to do. .txt files seemed like the best choice as every version of windows knows how to open it and has it free of charge.

Basically, i'm writing it because a) i want to learn VB.net and b) because i can't be arsed with the crappy little Excel workbook ive got to record all the scores.


If you know of a better way, than how i've done it, which by the sounds of it is anyway except this one.... please feel free.
Each of the text files hold name, phone number, matches: won, lost, played, points, days on holiday etc etc. Has to be able to work on a machine that has no other programs installed bar windows and this.


(and how can i sort it so it isn't messy?? what makes it messy..??)
 
To start at the end, what makes it messy is the number of completely unnecessary variables and references plus no obvious route through the maze. You really don't want to get into the habit of producing code like this ...

VB.NET:
[COLOR=#333333]EndIf[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next[/COLOR]


You remember when you started learning programming and everyone told you to comment as much as possible, to give variables and controls memorable and logical names, and you thought, "nah, I'll never need that sort of complication"? The chickens has entered the roost!

Ok, you can actually set up an Access database without Access (and your end user can access it without having Access too), using just VB. But if you don't want to go down that route there's still no obvious reason to have separate files for every player. However, if we leave the tidying up of the files for another day, it always makes sense to read all the data into the program from the very start and store it in a listbox or a list or an array or whatever for each data type you're going to use rather than be constantly reading files as the session progresses. Wherever possible keep your file reading and data initialisation in one sub-routine (and the same for file saving). This means that your references to the information will be consistent throughout the program, making it easier to read and follow, at the very least! It also means that what takes you 15 lines to code will as often as not be done in 3, as in the example I gave you. Obviously a program that works is more important than readability but you'd be surprised how often the two things go hand in hand!
 
After the line:
For i As Integer = 1 To 20


Try adding a message box to see if the variable is outputting the right number.
MsgBox(i.tostring)


Then you can see if the 'i' variable is stuffing up, or whether it is something wrong with your code after that, because I don't see any reason why it is not showing the first digit.

Regards,
Bryce Gough
 
Back
Top