LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the first folder name of path

I have a path and I wish to get the first folder name of that path.

I just want to get the first folder name, even if that folder changes, because I am reading all folders and files inside a directory.

the fisrt folder would be the folder inside the directory I chose with the dialog window, and this will be changing automatically.

Can someone please give me an idea on how to do this?

Thanks in advance!

0 Kudos
Message 1 of 16
(7,441 Views)

It would really help if you give an example of what you mean.  For example, for many Paths to user files, the first folder is "Users" or, if you want to include the Drive, it is C:\Users (since most User Files are inside the User's My Documents, found in C:\Users\<User Name>\Documents\...

 

I'm guessing you means something else, but I am not sure just what you want.  That's why I'm asking for examples.

 

Bob Schor

0 Kudos
Message 2 of 16
(7,432 Views)

Hi,

 

The attached VI takes a File Path as input, and returns the name of the folder containing the file. Is this what you are looking for?

0 Kudos
Message 3 of 16
(7,428 Views)

Here is an even simpler method, which works for Files and Folders (i.e. if File Path is, instead, a Folder Path, this returns the name of the "Folder's Folder").

 

Folder Name.png

 

There are two interesting "edge conditions" that one should examine -- What happens if one specifies a bare Drive (e.g. "C:\") or a file/folder on the root of a Drive ("C:\autoexec.bat")?  Both glstill's and my methods give a "correct" response of <Blank> to the bare drive.  However, glstill's method returns <blank> for a rooted file, while my method returns the drive letter, missing the colon (e.g. "C").

 

Yet another function you can use to take Paths apart and put them back together again is the Path to Array of Strings function.  This also returns a "Relative?" flag, which will be True if the Path starts with a Drive Letter.  This gives you a way to distinguish the two paths "C\autoexec.bat" (a relative path in a folder called "C" somewhere on some drive) and an absolute path ("C:\autoexec.bat") -- if the Relative flag is false (so you know the path is absolute) and there are only two array members (so you know the first, "C", is really a Drive Letter), you could add the colon back in, yourself.

 

I kind of like that method ...

 

Bob Schor

0 Kudos
Message 4 of 16
(7,392 Views)

Perhaps Path to Array of Strings would be useful here.

 

Edit: D'oh.  Should've read all the way through Bob's post.

--
Tim Elsey
Certified LabVIEW Architect
0 Kudos
Message 5 of 16
(7,364 Views)

I was trying to get a specific folder from a path. I already knew the name of one of the folders in the path and I extracted the name of the next folder.

I will leave the example.

0 Kudos
Message 6 of 16
(7,340 Views)

Nando88,

 

I recommend that you un-check your "Solution", as it is a "solution" to a completely different question (and, in fact, I'm not even sure I understand the question, seeing the "solution") than the one you posted.

 

The reason to mark a response as a "Solution" is to help other Forum readers who have a similar question.  Unfortunately, your "solution" has no bearing on the question "How to get the first folder name of path".

 

Bob Schor

0 Kudos
Message 7 of 16
(7,326 Views)

@Nando88 wrote:

I was trying to get a specific folder from a path. I already knew the name of one of the folders in the path and I extracted the name of the next folder.

I will leave the example.


That code makes no sense whatsoever! For example a "built array" followed by an "index array" just returns what you had in the first place. Nothing gained. There is a primitive to see if a path is empty, no need to jump through flaming hoops. Your FOR loop returns whatever happened in the last iteration, all other results of the loop code are irreversibly lost. As I said: Makes no sense!

 

If the suggested solutions don't fit your requrements, please improve the description of the problem.

 

What is the "first folder"? (e.g. Root folder? containing folder? subfolder of the current location?) The term "first" implies a sort order, but you don't define it!.

What is the "next folder"? Again we don't know what direction "next" is. What if there is more than one?

 

0 Kudos
Message 8 of 16
(7,305 Views)
If you want to know if a path is a file or a directory the File/directory info function (I think that's what it is called) will tell you that.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 9 of 16
(7,269 Views)

Without knowing exactly what it is you are asking for, I have created a VI which will extract all the folder names (include file name) into a string array, you could then use index array to acquire the specific folder you need, if it is the folder in which the file is stored, take the array length and use this with index array. If it is the root folder, index array of 0.

0 Kudos
Message 10 of 16
(7,248 Views)