Retrieving the parent folder name

J

John Dann

Is there any method in the File, Directory, FileInfo, Path etc classes
that makes it easy to retrieve the folder name of the immediate parent
folder of a file? I can't see one, but maybe I've missed it in all the
various overlapping methods and properties. For example, I might have
the file:

C:\Folder1\Folder2\Folder3\MyFile.txt

I want to be able to retrieve Folder3 into a string, no backslashes
etc, just 'Folder3'.

Obviously I can do this with various string search functions on the
full file path, but is this the only option or might there be a more
elegant approach

JGD
 
A

Armin Zingler

John said:
Is there any method in the File, Directory, FileInfo, Path etc classes
that makes it easy to retrieve the folder name of the immediate parent
folder of a file? I can't see one, but maybe I've missed it in all the
various overlapping methods and properties. For example, I might have
the file:

C:\Folder1\Folder2\Folder3\MyFile.txt

I want to be able to retrieve Folder3 into a string, no backslashes
etc, just 'Folder3'.

Obviously I can do this with various string search functions on the
full file path, but is this the only option or might there be a more
elegant approach

JGD


MsgBox(IO.Path.GetFileName(IO.Path.GetDirectoryName( _
"C:\Folder1\Folder2\Folder3\MyFile.txt") _
))



Armin
 
J

John Dann

MsgBox(IO.Path.GetFileName(IO.Path.GetDirectoryName( _
"C:\Folder1\Folder2\Folder3\MyFile.txt") _
))

The documentation isn't very clear but doesn't that return:

'C:\Folder1\Folder2\Folder3'

even though you might possibly expect GetDirectoryName to just return
the immediate folder name? I've still then got to do some string
searching to recover 'Folder3' so arguably I might as well do that
from the start.

JGD
 
J

Jay B. Harlow [MVP - Outlook]

John,
As Armin suggests I rely on System.IO.Path for all my path "searching"
needs, as this helps ensure that I follow all the rules for paths (such as
trailing \ or not, usage of \ or / as separators, & other esoteric rules
that a number of developers "roling their own" miss).

If you try Armin's code it works as he states! To see it in action, try the
following code:

Dim fileName As String = "C:\Folder1\Folder2\Folder3\MyFile.txt"
Debug.WriteLine(fileName, "fileName")

Dim directoryName As String = IO.Path.GetDirectoryName(fileName)
Debug.WriteLine(directoryName, "directoryName")

Dim parentName As String = IO.Path.GetFileName(directoryName)
Debug.WriteLine(parentName, "parentName")

The output will be:

fileName: C:\Folder1\Folder2\Folder3\MyFile.txt
directoryName: C:\Folder1\Folder2\Folder3
parentName: Folder3

Hope this helps
Jay

| Is there any method in the File, Directory, FileInfo, Path etc classes
| that makes it easy to retrieve the folder name of the immediate parent
| folder of a file? I can't see one, but maybe I've missed it in all the
| various overlapping methods and properties. For example, I might have
| the file:
|
| C:\Folder1\Folder2\Folder3\MyFile.txt
|
| I want to be able to retrieve Folder3 into a string, no backslashes
| etc, just 'Folder3'.
|
| Obviously I can do this with various string search functions on the
| full file path, but is this the only option or might there be a more
| elegant approach
|
| JGD
 
J

John Dann

As Armin suggests I rely on System.IO.Path for all my path "searching"
needs, as this helps ensure that I follow all the rules for paths (such as
trailing \ or not, usage of \ or / as separators, & other esoteric rules
that a number of developers "roling their own" miss).

OK thanks. I guess I was thinking of Path.GetFilename as a method that
knew what a filename was, whereas it just seems to be a particular
type of string function.

JGD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top