[RESOLVED] 03 Want to write the names of the text files in a folder into a string
I want to do one of two things. Either :
A.) Display all the text files that are in a certain folder. I know how to do this using OpenFileDialog code but I don't want the user to have all the options normally available to them when you see the open file dialog. I'd like them when they click a certain button to be able to see the names of all the files in a certain folder but not be able to open the files or browse to other folders, etc. etc. Imagine the open file dialog window without the browse option at the top and the open button and the text box to enter a file name etc.
Or
B.) I'd like to feed all the names of those text files in that certain folder which only has text files in it into a string. The names for all the text files have only numbers or letters or a dash or a decimal point in them. I'd probably want to put some other type of symbol like a + maybe or something else that doesn't fall into the other categories to mark where one file name ends and the next begins or else feed the file names into separate strings that were in an array.
After that I'd know what to do to process the one big string or the group of separate strings to do what I want. If no one answers this I'll figure it out on my own but maybe someone could point me in the right direction here and reduce the amount of searching around that I have to do.
VBForums has helped me a lot and I know that it's helped many other programmers a lot. God bless you VBForums. :duck:
Re: 03 Want to write the names of the text files in a folder into a string
The Directory.GetFiles method will give you a list of files in a folder, allowing you to optionally filter them by name. Once you have that array you can do what you like with it, e.g. display it in a ListBox or ComboBox, or write it out to a file or whatever. Note that each string is a fully qualified path. The Path.GetFileName or Path.GetFileNameWithoutExtension will trim it to just the file name if desired.
Re: 03 Want to write the names of the text files in a folder into a string
this will populate a listbox with a folders mp3 files
Code:
'populate listbox's with song names
Try
Dim tmp As String = (Application.StartupPath & "\FOLDER NAME HERE")
Dim Files() As String
Files = System.IO.Directory.GetFiles(tmp, "*.mp3 ")
For Each str As String In Files
Me.listbox1.Items.Add(IO.Path.GetFileNameWithoutExtension(str))
Next
Catch ex As Exception
Me.Hide()
MsgBox(ex.Message)
End Try
HTH
Re: 03 Want to write the names of the text files in a folder into a string
Thanks to both of you. You both get As for your replys. Got everything working the way I want it to. Learned some interesting things today. I was using file.exists to try to check if a folder exists. It was always evalutating false and kept on trying to create a folder that already existed. This didn't cause any problem that I noticed but I learned today that the code wasn't working the way I thought it was so I fixed things so it didn't keep on trying to create the folder that it had already created the first time. I learned that in the code
Code:
dim word as string = "abcdef"
dim a as integer
a = word.IndexOf("g") ' a will be set = to -1 because there isn't a g
a = word.IndexOf("c") ' a wiil be set = to 2 since index starts at 0 for first character
So by using IndexOf it will identify the starting point of a shorter string within a larger string and it will also tell you if the shorter string isn't present in the larger string. This was helpful in the code I was creating today.