Reputation: 119
I have a folder titled ‘The Arts’ which contains various sub-folders, one of which is titled ‘Music’. This ‘Music’ sub-folder contains various text files in the format:
John Doe.TXT
John Lennon.TXT
Elton John.TXT
Now, on my Form, I have two Textboxes in which the user can enter the names of artists like so;
Textbox1.Text = John
Textbox2.Text = Lennon
What I want to achieve is that on clicking a button on this form, the program searches the ‘The Arts’ parent folder for the ‘Music’ sub-folder and then searches within this music sub-folder for the text file name which exactly matches the artist name concatenated from Textboxes 1 and 2.
If a text file name exactly matches the artist name concatenated from Textboxes 1 and 2, then display a message. If no text file name within the Music sub-folder matches the name concatenated from Textboxes 1 and 2; then display a message that no file is found.
The below code is incomplete and just shows how I specified the main file path. I do not know how to proceed to get the program to do the above.
I am using Visual Basic 2010 Express. Thank you for your help.
Dim FilePath As String
FilePath = (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "The Arts\"))
'This section is where I am stuck and need help...Thank you in advance.
If File.Exists(FilePath) Then
MsgBox("File found.")
Else
MsgBox("A record does not exist for this artist.")
Exit Sub
End If
Upvotes: 0
Views: 781
Reputation: 13224
How to check if a text file name exactly matches the artist name concatenated from Textboxes 1 and 2
You need to first concatenate the text from the text boxes, that given your example, need to be separated by a space. There are a few ways to accomplish that.
For example like this:
Dim artistName = TextBox1.Text + " " + TextBox2.Text
Or this:
Dim artistName = String.Concat(TextBox1.Text, " ", TextBox2.Text)
And there are even more ways to do this.
Next you will need to assemble this into a full file path name. For readability it makes sense to do this in a few steps:
' Directory
Dim desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim musicPath = Path.Combine(deskTopPath, "The Arts", "Music"))
' Combine directory name and the name of the file we want to find.
Dim filePath = Path.Combine(musicPath, artistName + ".TXT")
Finally you can check whether that file exists by calling the File.Exists
method.
Dim found = File.Exists(filePath)
Upvotes: 2