LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to get file name from string path

Solved!
Go to solution

I have a file path, which I convert to a string.

 

Example: C:\Test Path 1\Location 1\Location 2\This_Is_The_File_I_Want-1234.elf

 

I am trying to use the Match Regular Expression Function to search for the value of the file name between the last "\" and before ".elf". I am unsure how to configure it so that it will search between those two search parameters to return the file name.

 

Does anyone have a better method for searching for the name of that file? It can change over time, hence wanting to dynamically search for it.

0 Kudos
Message 1 of 7
(1,955 Views)

First of all, don't convert it to a string:

billko_0-1706799255301.png

 

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 7
(1,946 Views)

Sorry, Nevermind.

 

0 Kudos
Message 3 of 7
(1,945 Views)

In addition to what billko suggested, you can use the Get File Extension vi from the Advanced File Functions palette. This vi gives you the filename without extension as you require.

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 4 of 7
(1,899 Views)
Solution
Accepted by topic author LearningLabVIEW

@LearningLabVIEW wrote:

Does anyone have a better method for searching for the name of that file? It can change over time, hence wanting to dynamically search for it.


Yes, as mentioned.

 

As for the regular expression, there are tons of variations to get similar results.

 

Following is for educational purposes.

 


@LearningLabVIEW wrote:

I am trying to use the Match Regular Expression Function to search for the value of the file name between the last "\" and before ".elf"


This returns anything* between the last "\" and before ".elf", but it does anchor .elf to the end of the string (the "$").

 

wiebeCARYA_0-1706808905666.png

* Anything, including ".elf". So  "C:\Test Path 1\Location 1\Location 2\This_Is_The_.e;lfFile_I_Want-1234.elf" returns "This_Is_The_.elfFile_I_Want-1234" as the first ".elf" isn't anchored to the end.

 

If you'd really want to match anything between the last \ and .elf the regex get a lot more complicated.

0 Kudos
Message 5 of 7
(1,870 Views)

Here's a combination of some of the suggestions:

 

altenbach_0-1706811281719.png

 

 

And, as has been mentioned, if you already have it as "path" datatype, it never needs to be converted to a string in the first place.  A full path is OS independent, but once you convert it to a string, it will be OS specific and the code will break on other platforms.

0 Kudos
Message 6 of 7
(1,858 Views)

@pincpanter wrote:

In addition to what billko suggested, you can use the Get File Extension vi from the Advanced File Functions palette. This vi gives you the filename without extension as you require.


Oops, I forgot about that part of the requirement.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 7 of 7
(1,843 Views)