r/visualbasic • • Jun 05 '21

VB.NET Help How would I create a Find and Replace system?

I would like to create a sort of hardcoded "Find & Replace" system. Currently in an app I have, all items loaded in a list (items are folder names) will display in a ComboBox. A couple of the names are not accurate, because they contain illegal characters, or characters not allowed as folder names. Therefore, I would like create either a two-dimensional list or array, I'm not sure which. The first column would basically be "Find what" and the second column would be "replace with". What's the best way to create a system that will do this?

See the table for sample data.

Find WhatReplace With
First-NameFirst/Name
Second- NameSecond: Name

As you can see, any time there is an illegal character, it replaces it with a dash, thus just searching for the dash and replacing it with the same character each time would not work.

I can probably figure out how to implement it, myself, once I know the best way to define it.

5 Upvotes

6 comments sorted by

1

u/RJPisscat Jun 05 '21

Is this the same music app you've been working on? If so, have you changed the hierarchy of the file system to folders with Artist Name/Album Name/ and under that a set of pictures with standard names like "front-cover.jpg" and "back-cover.jpg" and so forth? And if so, is this question about how to display the names of bands and albums in a human-legible format instead of the format forced on you by limitations of the file-naming system?

If so, are you cataloging the bands/albums in a database, or using the file system as the catalog? If the latter, you can put an Info.Txt (or better yet, Info.Xml or Info.Json) file inside each of those directories. If the file exists in a subdirectory, it has information about display name of the artist or album, and by using an Info file in one of those formats (json is the most portable of those three), you can later add items you may want to store in addition, such as the names of the band members, or the playing time of an album. If you take this approach it's something you'd want to do on the fly as each folder is created, and now at this stage of development you want to go back and fix the names you've already created while testing this app, and the question makes sense for doing that.

Am I correct? If not, set me straight, and if so, let's work from there.

1

u/mudderfudden Jun 05 '21

Yes, it's the same project. I actually like the idea of the text file. I haven't really had the chance to really understand the previous block of code your gave me, where you mention to "unflatten" my code. But anyways, right now I have this in my Form_Load event to populate my Artist ComboBox:

LoadBandInfo()

For Each key In BandInfo.Keys

cboArtist.Items.Add(key)

Next

I imagine somehow I can use key as part of the path to see if there's a file named name.txt in that specific Artist directory, read the file and replace the specific key (or dictionary item in this case) with the file contents (which should be one line). Would that work? If so, that'd be easy enough.

1

u/RJPisscat Jun 05 '21

Yes, that will work.

Whatever about the other code you don't understand, let's go through it if you want.

1

u/mudderfudden Jun 05 '21

I'll do that some other time. I'll reply on that post when I'm able to get to it and I still have questions.

Meanwhile, based on the first LoadBandInfo() subprocedure, I went ahead and did this:

For Band Names

If My.Computer.FileSystem.FileExists(BandNamdDirectory & "\name.txt") Then

Dim objReader As New System.IO.StreamReader(BandNamdDirectory & "\name.txt")

BandName = objReader.ReadLine()

End If

For Album Names

If My.Computer.FileSystem.FileExists(AlbumDirectory & "\name.txt") Then

Dim objReader As New System.IO.StreamReader(AlbumDirectory & "\name.txt")

AlbumName = objReader.ReadLine()

End If

It took a bit of testing to figure out which variables were doing what, exactly, basically msgbox(variablename), but I eventually figured that out.

Thought there was a way to shorten My.Computer.FileSystem.FileExists and System.IO.StreamReader, but I can't think of it right now. Thanks again.

1

u/RJPisscat Jun 05 '21

You've tested so you know it works. It's good.

When creating path names, I recommend using

IO.Path.Combine(BandNameDirectory, "name.txt")

which is the same as

IO.Path.Combine(BandNameDirectory, "\name.txt")

and the reasons I recommend it are:

  • It doesn't care if you get the slashes correct. It fixes them for you.

  • It's immediately legible to the reader.

Also I would ensure that FileExists and New StreamReader are using the same full path (in case you change one or the other, and for legibility) and thus:

Dim FullPathName As String = IO.Path.Combine(BandNameDirectory, "\name.txt")
If IO.File.Exists(FullPathName) Then
    Using objReader As New IO.StreamReader(FullPathName)
        BandName = objReader.ReadLine()
    End Using
End If        

Remember, your code already works, and you don't have to take these suggestions. There is always more than one way to write code that works the way you want, and no "right" way, but there are preferred ways.

I recommend "Using" because it calls Dispose automatically on the object, and in this case closes the file so that others (people, applications) can access it. Also, if you have a Try/Catch outside a Using block, and an Exception is thrown during the Using block, the resources for the StreamReader will be properly disposed of and the file will be property closed.

You're doing well! Keep it up. You're sticking with this project, I'm happy for you.

1

u/mudderfudden Jun 06 '21

Thank you, sir! I appreciate your assistance along the way, you may already know that but I thought I'd say it anyways.

I'm always up for learning new ways of doing things, particularly if they're more efficient. I'll definitely look into this code, as well.