6 Replies - 10219 Views - Last Post: 16 September 2009 - 05:19 PM Rate Topic: -----

#1 rainbow evil  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 308
  • Joined: 15-July 09

Find Strings within notepad text file

Posted 15 September 2009 - 10:25 AM

Hi,

I am trying to write login page code for the login button but can't find how to find a string in a .txt file. The only code I have managed to find on the subject is opening up the text file with the
FileOpen(1, "File location", OpenMode.Output)
code, but even this would be best removed as it would open the text file containing all usernames and passwords within the file.

I need the system to search for a specific string within a file (Attached File  Usernames___Passwords.txt (159bytes)
Number of downloads: 253), and with that string find the password associated with it (in my attached example the letters/words after the apostrophe) but stopping at the end of the line.

Thanks in advance

Rainbow Evil

Is This A Good Question/Topic? 0
  • +

Replies To: Find Strings within notepad text file

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Find Strings within notepad text file

Posted 15 September 2009 - 10:55 AM

untested pseudo-code
Dim FileContents() As String = IO.File.ReadAllLines(<full name and path to file here>)

For I = 0 to FileContents.Length - 1
  Dim LineParts() as String = FileContents(i).Split("'")
  If LineParts(1) = InputUsername Then
	If LineParts(2) = InputPassword Then
	  ' successful login
	Else
	  ' shoot the burglar
	End If
  End If
Next


then you can iterate through the FileContents array of strings - use .Split"'" on each string to break it at the ' characters - it should give 3 segments, an empty string for stuff before the first ' and the username and the password. Now search the array for a matching user name and then compare to the expected password.
Was This Post Helpful? 1
  • +
  • -

#3 rainbow evil  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 308
  • Joined: 15-July 09

Re: Find Strings within notepad text file

Posted 15 September 2009 - 04:50 PM

View Postmark.bottomley, on 15 Sep, 2009 - 09:55 AM, said:

untested pseudo-code
Dim FileContents() As String = IO.File.ReadAllLines(<full name and path to file here>)

For I = 0 to FileContents.Length - 1
  Dim LineParts() as String = FileContents(i).Split("'")
  If LineParts(1) = InputUsername Then
	If LineParts(2) = InputPassword Then
	  ' successful login
	Else
	  ' shoot the burglar
	End If
  End If
Next


then you can iterate through the FileContents array of strings - use .Split"'" on each string to break it at the ' characters - it should give 3 segments, an empty string for stuff before the first ' and the username and the password. Now search the array for a matching user name and then compare to the expected password.


Thanks a million for this code! Most of the login coding I have found I did not understand what each bit of code did and therefore I didn't use them because it's effectively stealing code and also I like to know how things work (especially if I am making them!). This code on the other hand makes it perfectly clear what each line is doing and allows me to feel satisfied in knowing how it works.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 rainbow evil  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 308
  • Joined: 15-July 09

Re: Find Strings within notepad text file

Posted 15 September 2009 - 05:24 PM

On the same topic I have unfortunately encountered another possible problem :(

I now need to restrict users from entering the apostrophe character when creating an account to prevent the apostrophe confusing the system. I am unsure of how to bring up an error for if a string contains a certain character, does anybody else know?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 376
  • Joined: 17-July 09

Re: Find Strings within notepad text file

Posted 15 September 2009 - 06:31 PM

You can use regular expressions to limit what charactors users can input (and probably the best way) but here's a quick on how to notify the user of an invalid Comma (you can change it tho).

		If (TextBox1.Text.Contains(",")) Then
			MessageBox.Show("invalid charactor")
		End If


Was This Post Helpful? 1
  • +
  • -

#6 rainbow evil  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 308
  • Joined: 15-July 09

Re: Find Strings within notepad text file

Posted 16 September 2009 - 02:42 PM

Thanks for all help so far on my topic.

One final thing on the topic. To create a new account I have created a "create account" page with some elaboration on the original text document (it now has 6 textboxes; username, 2 for password, email address, security question and security answer). I have created all the validation code I require:
Public Class Form2

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

		Dim FileContents() As String = IO.File.ReadAllLines("<text file path>")
		Dim found1 As Boolean

		If TextBox1.Text = "" Then
			Label3.Text = "No username entered"
			Label3.ForeColor = Color.Red
		ElseIf TextBox1.Text.Contains("'") Then
			Label3.Text = "Invalid character(s)"
			Label3.ForeColor = Color.Red
		Else
			For I = 0 To FileContents.Length - 1
				Dim LineParts() As String = FileContents(I).Split("'")
				If LineParts(1).ToUpper = TextBox1.Text.ToUpper Then
					Label3.Text = "Username already in use"
					Label3.ForeColor = Color.Red
					found1 = True
				End If
			Next
			If found1 = True Then
			Else
				Label3.Text = "Available"
				Label3.ForeColor = Color.Green
			End If
		End If
	End Sub


	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		If TextBox2.TextLength < 6 Then
			Label4.Text = "Password too short"
			Label4.ForeColor = Color.Red
		ElseIf TextBox2.Text.Contains("'") Then
			Label4.Text = "Invalid character(s)"
			Label4.ForeColor = Color.Red
		ElseIf TextBox2.Text = TextBox3.Text Then
			Label4.Text = "Acceptable password"
			Label4.ForeColor = Color.Green
		Else
			Label4.Text = "Passwords do not match"
			Label4.ForeColor = Color.Red
		End If
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim FileContents() As String = IO.File.ReadAllLines("<text file location>")
		Dim found2 As Boolean

		If TextBox4.Text = "" Then
			Label9.Text = "No Email entered"
			Label9.ForeColor = Color.Red
		Else
			For I = 0 To FileContents.Length - 1
				Dim LineParts() As String = FileContents(I).Split("'")
				If LineParts(3).ToUpper = TextBox4.Text.ToUpper Then
					Label9.Text = "Email already in use"
					Label9.ForeColor = Color.Red
					found2 = True
				End If
			Next
			If found2 = True Then
			ElseIf TextBox4.Text.Contains("@") Then
				Label9.Text = "Acceptable Email"
				Label9.ForeColor = Color.Green
			Else
				Label9.Text = "Not an Email address"
				Label9.ForeColor = Color.Red
			End If
		End If
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

		Dim fail As Boolean
		Dim FileContents() As String = IO.File.ReadAllLines("<text file location>")
		Dim found1 As Boolean
		Dim found2 As Boolean

		If TextBox1.Text = "" Then
			Label3.Text = "No username entered"
			Label3.ForeColor = Color.Red
			fail = True
		ElseIf TextBox1.Text.Contains("'") Then
			Label3.Text = "Invalid character(s)"
			Label3.ForeColor = Color.Red
			fail = True
		Else
			For I = 0 To FileContents.Length - 1
				Dim LineParts() As String = FileContents(I).Split("'")
				If LineParts(1).ToUpper = TextBox1.Text.ToUpper Then
					Label3.Text = "Username already in use"
					Label3.ForeColor = Color.Red
					fail = True
					found1 = True
				End If
			Next
			If found1 = True Then
			Else
				Label3.Text = "Available"
				Label3.ForeColor = Color.Green
			End If
		End If
		If TextBox2.TextLength < 6 Then
			Label4.Text = "Password too short"
			Label4.ForeColor = Color.Red
			fail = True
		ElseIf TextBox2.Text.Contains("'") Then
			Label4.Text = "Invalid character(s)"
			Label4.ForeColor = Color.Red
			fail = True
		ElseIf TextBox2.Text = TextBox3.Text Then
			Label4.Text = "Acceptable password"
			Label4.ForeColor = Color.Green
		Else
			Label4.Text = "Passwords do not match"
			Label4.ForeColor = Color.Red
			fail = True
		End If
		If TextBox4.Text = "" Then
			Label9.Text = "No Email entered"
			Label9.ForeColor = Color.Red
			fail = True
		Else
			For I = 0 To FileContents.Length - 1
				Dim LineParts() As String = FileContents(I).Split("'")
				If LineParts(3).ToUpper = TextBox4.Text.ToUpper Then
					Label9.Text = "Email already in use"
					Label9.ForeColor = Color.Red
					fail = True
					found2 = True
				End If
			Next
			If found2 = True Then
			ElseIf TextBox4.Text.Contains("@") Then
				Label9.Text = "Acceptable Email"
				Label9.ForeColor = Color.Green
			Else
				Label9.Text = "Not an Email address"
				Label9.ForeColor = Color.Red
				fail = True
			End If
		End If
		If TextBox5.Text = "" Or TextBox6.Text = "" Then
			fail = True
		End If
		If fail = True Then
			MsgBox("Please enter all the required information correctly to create the account, ensuring that they are available.")
		Else
*****************
		End If
	End Sub
End Class



I now need help with some code to go where the asterisks are which will create a new line in the text file with textbox1.text first followed by an apostrophe, then textbox2 followed by an apostrophe, then textbox4, textbox5 and textbox 6 in the same manner.

I did find some code which uses a "write" method but, from reading, it seems it adds quotes on either side. In addition I can't find how to create a new line on the file even using this method so if someone knows a method which will allow me to create a new line with all the data I require separated by apostrophe's then I would be very grateful if they could show me it!

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 376
  • Joined: 17-July 09

Re: Find Strings within notepad text file

Posted 16 September 2009 - 05:19 PM

Use streamwriter and WriteLine.

		Using writer As StreamWriter = New StreamWriter("C:\YOURFILE.ext", True)
			writer.WriteLine("'" & TextBox1.Text & "'" & TextBox2.Text & "'" & TextBox3.Text & "'" _
							 & TextBox4.Text & "'" & TextBox5.Text & "'" & TextBox6.Text)
		End Using



This will add the items from the textboxes to a file (Replace YOURFILE.ext with your file) in the format of 'textbox1'textbox2'textbox3.....

If you get an error about Streamwriter not being declared, add Imports System.IO to the Imports.

This post has been edited by PDUNZ: 16 September 2009 - 05:21 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1