Hello,
I have another request.
How to use regexp to get the folder from a path of indeterminate length?
Some code was recently provided by tass_co, but it seems to only isolate a file rather than a folder.
I am hoping to use this idea to display a folder name on mouse over, not really to create a shortcut. I'm not sure if that matters, but this is just a cosmetic notion rather than a practical application.
Thanks for any help.
It is currently March 28th, 2024, 10:37 am
Another regexp question - how to get the folder name in a path?
- balala
- Rainmeter Sage
- Posts: 16109
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Another regexp question - how to get the folder name in a path?
For instance:
Code: Select all
[Variables]
File=c:\Program Files\Rainmeter\Rainmeter.exe
[MeasureFolder]
Measure=String
String=#File#
RegExpSubstitute=1
Substitute="^(.*)\\(.*)$":"\1"
Note that if you replace the \1 element of the Substitute option with \2 (Substitute="^(.*)\\(.*)$":"\2"), you get the name and extension of the file.
Re: Another regexp question - how to get the folder name in a path?
Hey, thanks balala! It works great.balala wrote: ↑August 28th, 2022, 6:45 pm For instance:I obviously believe the path of the file is returned by probably either a WebParser, or a FileView plugin measure, but the basic is the same. Test the code please and let me know if it works as expected.Code: Select all
[Variables] File=c:\Program Files\Rainmeter\Rainmeter.exe [MeasureFolder] Measure=String String=#File# RegExpSubstitute=1 Substitute="^(.*)\\(.*)$":"\1"
Note that if you replace the \1 element of the Substitute option with \2 (Substitute="^(.*)\\(.*)$":"\2"), you get the name and extension of the file.
However, this gets the root folder rather than the destination folder.
So for this path (example) H:\Movies\TV Shows\ it would get "TV Shows\" but the path length would hopefully be inconsequential, and any destination folder would be returned. Can it be done that way?
Re: Another regexp question - how to get the folder name in a path?
CodeCode wrote: ↑August 28th, 2022, 7:15 pm Hey, thanks balala! It works great.
However, this gets the root folder rather than the destination folder.
So for this path (example) H:\Movies\TV Shows\ it would get "TV Shows\" but the path length would hopefully be inconsequential, and any destination folder would be returned. Can it be done that way?
Code: Select all
Substitute="^.*\\(.*)\.*$":"\1"
Re: Another regexp question - how to get the folder name in a path?
Heya tass_co! That only returns a "\1" instead of a destination folder. It would also make sense if the backslash were included in the returned string. "Movies\"
Thanks for offering your help as well.
Re: Another regexp question - how to get the folder name in a path?
Code: Select all
Substitute="^.*\\(.*)\.*$":"\1\"
Re: Another regexp question - how to get the folder name in a path?
Sorry to be a pain. but that returns "\1\"
- balala
- Rainmeter Sage
- Posts: 16109
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Another regexp question - how to get the folder name in a path?
Sorry I thought you'll use the substitution ONLY for paths of files. My bad...
But now I'm not sure. For files I think my susbtitution does work. But what should it return for instance for the H:\Movies\TV Shows\ path? Sorry, but I didn't understand (my bad again)...