Wednesday, April 15, 2009

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


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

    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. Hi Naveen, I can not help you making your projects. However, if you have any enquiry, feel free to tell your problem here ^^



Related Posts

Dual Axis not Working in Tableau

Problem : When creating chart with Dual Axis, Synchronize Axis is disabled and cannot be clicked. Cause : Tableau identified this as ...

About Me

My photo
Is an ordinary unNoticed man, with a little knowledge to share and high dreams to achieve. I'd be glad if I can help others, 'coz the only thing for the triumph of evil is for a good man to do nothing. I like English and Chinese language, and know a little about them. Hence, for me, most things can best be expressed in Chinese, some in English, and the rest in others ^^

About Blog

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.

Popular Posts



Follow by Email