473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Retrieving the parent folder name

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
Nov 21 '05 #1
4 6901
John Dann schrieb:
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.GetDirectoryNam e( _
"C:\Folder1\Folder2\Folder3\MyFile.txt") _
))

Armin
Nov 21 '05 #2
On Sun, 10 Jul 2005 13:25:39 +0200, Armin Zingler
<az*******@freenet.de> wrote:

MsgBox(IO.Path.GetFileName(IO.Path.GetDirectoryNa me( _
"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
Nov 21 '05 #3
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

"John Dann" <ne**@prodata.co.uk> wrote in message
news:57********************************@4ax.com...
| 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
Nov 21 '05 #4
On Sun, 10 Jul 2005 08:05:32 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
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
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason | last post by:
I have an XML web control on a usercontrol. The only purpose for this usercontrol is to get XML data from a URL and display it in the xml web control with the help of XSL. I can get this to...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
3
by: moondaddy | last post by:
How do you get the name of a folder's parent folder? Here's how I get the folder name who I want to get it's parent's name: Dim ClientPath As String = Directory.GetCurrentDirectory thanks. ...
1
by: Glenn T. Kitchen | last post by:
Dear Group, I'm having problems retrieving the child rows of a parent row. The parent table is Users and the child table is Addresses. I used the Schema to create a DataRelation between the...
1
by: Brian P | last post by:
I have a class, Folder, that I want to hold a reference to its parent Folder as well as its subfolders. I'm using List<Folder> to hold its list of sub folders. What I want to do is when a...
4
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method...
5
by: Randy Smith | last post by:
Hi ALL, I wonder if anyone has been using n-tier to bind to a GridView control by using the ObjectDataSource. This is our first OOP web application, and we have no tables. Right now we are...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
0
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.