Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get directory name of directory containing directory

How to get the folder name from the full path of folder?

This is file path,

"c:\projects\roott\wsdlproj\devlop\beta2\text"

Here text is the folder name.

But i want to get the folder containing text, that is beta2

like image 603
Smith Avatar asked Nov 17 '11 20:11

Smith


People also ask

How do I get the fileName in a directory in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

What is System IO path?

A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.

How do I find my working directory in Python?

To find out which directory in python you are currently in, use the getcwd() method. Cwd is for current working directory in python. This returns the path of the current python directory as a string in Python. To get it as a bytes object, we use the method getcwdb().


2 Answers

The Path.GetDirectoryName method can be used to return "c:\projects\roott\wsdlproj\devlop\beta2", as shown below:

Dim filePath As String = "c:\projects\roott\wsdlproj\devlop\beta2\text"
Dim directory As String = Path.GetDirectoryName(filePath)

To get just the name of the parent folder, "beta2", you can split the input and take the second last entry, given that the input is indeed accurate:

Dim split As String() = filePath.Split("\")
Dim parentFolder As String = split(split.Length - 2)
like image 125
Ahmad Mageed Avatar answered Sep 29 '22 01:09

Ahmad Mageed


Fri 7/09/2012 10:42 AM io.path.getFileName(filePath) will return the folder name

like image 22
Xardax99 Avatar answered Sep 29 '22 03:09

Xardax99