No More Solutions

You can find a lot of debugging and deploying problems while developing applications in .NET and Visual Basic here. There are also some querying tips in SQL and typical source codes which might be useful shared here.

Get file name from a path in VB

One of my friend once asked how to get file name from a path. There're actually a lot of ways to do it. I'll try a simple one, using Split and UBound function.

e.g. the path is C:\Test\DeeperPath\EvenDeeperPath\Test.txt
and we're going to get the file name, i.e. Text.txt

The name of the controls:
textbox : Text1, containing the path

Some things we have to know:
1. To split the path into array: Split(Text1.Text, "\")
We'll get array of string:
Split(Text1.Text, "\")(0) = C:
Split(Text1.Text, "\")(1) = Test
Split(Text1.Text, "\")(2) = DeeperPath
Split(Text1.Text, "\")(3) = EvenDeeperPath
Split(Text1.Text, "\")(4) = Test.txt

2. To get upper bound of an array: UBound(array)

So, to get the file name from a path, we just need a line of code:
Split(Text1.Text, "\")(UBound(Split(Text1.Text, "\")))

The result will be: Test.txt
Bookmark and Share

2 comments:

  1. Anonymous said...
     

    hi i am Naveen soni new vb developer give me your gmail id for chat my gmail id is jaihons@gmail.com

    i dont understand this code how it work i founded manny codes on net but not any working i have manny projects to work in vb can you help me for making or solving my problems please reply me?

  2. Steven Luck said...
     

    Hi Naveen, I can not help you making your projects. However, if you have any enquiry, feel free to tell your problem here ^^

Post a Comment