05-04-2016 09:08 PM
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!
05-04-2016 09:31 PM
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
05-04-2016 09:35 PM
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?
05-05-2016 06:40 AM
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").
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
05-05-2016 10:25 AM - edited 05-05-2016 10:27 AM
Perhaps Path to Array of Strings would be useful here.
Edit: D'oh. Should've read all the way through Bob's post.
05-05-2016 12:44 PM
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.
05-05-2016 01:04 PM
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
05-05-2016 03:57 PM
@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?
05-06-2016 05:39 PM
05-07-2016 06:41 AM
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.