String functions

ninel

Active member
Joined
Mar 23, 2005
Messages
32
Location
Land O Lakes, Florida
Programming Experience
3-5
I have the following path of a file:
"L:\VoicenetSQL\Project\Tampa\Politic\AT0000\Test.csv"
I need to pull out the folder name where the file resides. In this case I would need "AT0000".

How can I use the Instr function to do this? Or can I use something else?

Thanks,
Ninel
 
VB.NET:
Dim str As String '= your file path string       
Dim spl As String() = str.Split("\")
This will split the string into a single dimension array. Each index of the array will contain the characters between the "\" marks.
first cell will be "L:" second will be "VoicenetSQL" etc, etc...
You will need to determine the number of entries in the array and then access the next to last value to get the folder name.

I'm not at my terminal right now, but this approach should work.

Research on "string.Split" and you will find help.
 
Last edited:
The Instr function is legacy. The .NET replacement is System.String.Substring.
But since you are dealing with paths, it would be best to use the System.IO.Path class. An example:
VB.NET:
Dim str As String = "L:/VoicenetSQL/Project/Tampa/Politic/AT0000/Test.csv"
Dim dir As String = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(str))
 
You can also work with FileInfo and DirectoryInfo objects:

Dim fi as New FileInfo("L:\VoicenetSQL\Project\Tampa\Politic\AT0000\Test. csv")
MessageBox.Show(fi.Directory.Name)

FI and DI have other accessory methods for determining existence of files and opening them for reading/writing, moving and deleting. Quite useful objects!
 
In 2005 you also have the My Namespace....
VB.NET:
Dim MyString As String = "c:\this\is\a\path\file.txt"
MessageBox.Show(My.Computer.FileSystem.GetFileInfo(MyString).Directory.Name)
 
Is this a factory method for a FileInfo object?

It seems that:

VB.NET:
Dim fi as FileInfo = My.Computer.FileSystem.GetFileInfo("L:\VoicenetSQL\Project\Tampa\Politic\AT0000\Test.csv")
Dim fi as New FileInfo("L:\VoicenetSQL\Project\Tampa\Politic\AT0000\Test.csv")

are equivalent.. However, if GetFileInfo is indeed a factory method then care should be excercised to avoid falling into the trap of repeatedly calling it if several operations are to be performed; the behaviour of the factory might be that it will return the same object each time it is called, but the risk is that it will return a new object targeting the same file each time. To repeatedly establish a variable in memory is a resource waste that is usually avoided by caching the method return value


i.e. for factory methods, if you are unsure of whether the factory caches the response, then do not do this:

VB.NET:
myFactory.factoryMethod(arg).DoSomethingWithReturnedObject()
myFactory.factoryMethod(arg).DoSomethingElseWithReturnedObject()
myFactory.factoryMethod(arg).DoSomethingElseTooWithReturnedObject()

instead, do this:
VB.NET:
Dim cachedObject as Type = myFactory.factoryMethod(arg)
cachedObject.DoSomethingWithReturnedObject()
cachedObject.DoSomethingElseWithReturnedObject()
cachedObject.DoSomethingElseTooWithReturnedObject()


*note; i dont put spaces in the code(courier font) sections; the forum does that to prevent long-line-attacks
 
Last edited by a moderator:
Back
Top