Hello,
I'm writing code that will populate a listbox with the contents of a folder (a list of excel and word files). Naturally, the listbox has all file names with the ".xls" or ".doc" extension included. How can I get rid of this? I have tried

fileName.Remove()

and

fileName.Replace()

functions with no success. I can not specify the length of the file name, as they are all different and constantly changing. The only thing I know for sure is that the last four characters need removed, and then the remaining string to be put in the listbox. Any suggestions? Thank you in advance for any help!

Edited 6 Years Ago by bklynman01: n/a

You can use the "Replace" function as in -

public Function StripOut(From as string, What as string) as string

    Dim i as Integer

    StripOut = From
    for i = 1 to len(What)
        StripOut = Replace(StripOut, mid$(What, i, 1), "")
    next i

End Function

'Just place it somewhere in your code (preferably in a module), and call it like this:

Text1.Text = StripOut("abcdefg", "bdf")
'This will return a string that had all of its 'b', 'd', and 'f' characters removed.

If you only want to remove the last say 3 characters or text, use the "RTrim" function -

Text1.Text = RTrim(Text1.Text,3)
'This will remove the last 3 characters.

Thanks Walt! I should have said it earlier, but I'm using VB.net 1.1 - I'm trying to upgrade! The code I ended up using is:

Me.strFile = Microsoft.VisualBasic.Left(strFile, strFile.Length - 4)

This will ensure that no matter what the extension is, it will not show in the box!

Edited 6 Years Ago by bklynman01: n/a

Thanks Walt! I should have said it earlier, but I'm using VB.net 1.1

Then why did you post in the VB6 forum?

The code I ended up using is:

Me.strFile = Microsoft.VisualBasic.Left(strFile, strFile.Length - 4)

This will ensure that no matter what the extension is, it will not show in the box!

Really? What if the file name is test.pl or trial.x ? :icon_wink:

Hello,
I'm writing code that will populate a listbox with the contents of a folder (a list of excel and word files). Naturally, the listbox has all file names with the ".xls" or ".doc" extension included. How can I get rid of this? I have tried

fileName.Remove()

and

fileName.Replace()

functions with no success. I can not specify the length of the file name, as they are all different and constantly changing. The only thing I know for sure is that the last four characters need removed, and then the remaining string to be put in the listbox. Any suggestions? Thank you in advance for any help!

Try using something like

FileName = Left$(FileName,LEn(FileName)-4)

This will change "Test.xls" to "Test".

John, as you can see at the bottom, this thread was solved, which means the solution is above.:)

This question has already been answered. Start a new discussion instead.