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 April 28th, 2024, 12:08 pm
Another regexp question - how to get the folder name in a path?
-
- Posts: 1366
- Joined: September 7th, 2020, 2:24 pm
- Location: QLD, Australia
Another regexp question - how to get the folder name in a path?
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
-
- Rainmeter Sage
- Posts: 16183
- 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.
-
- Posts: 1366
- Joined: September 7th, 2020, 2:24 pm
- Location: QLD, Australia
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?
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
-
- Posts: 517
- Joined: May 4th, 2020, 3:01 pm
- Location: Ankara, TURKEY
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"
I don't know where i going from here, but i promise it won't be boring...
-
- Posts: 1366
- Joined: September 7th, 2020, 2:24 pm
- Location: QLD, Australia
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.
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
-
- Posts: 517
- Joined: May 4th, 2020, 3:01 pm
- Location: Ankara, TURKEY
Re: Another regexp question - how to get the folder name in a path?
Code: Select all
Substitute="^.*\\(.*)\.*$":"\1\"
I don't know where i going from here, but i promise it won't be boring...
-
- Posts: 1366
- Joined: September 7th, 2020, 2:24 pm
- Location: QLD, Australia
Re: Another regexp question - how to get the folder name in a path?
Sorry to be a pain. but that returns "\1\"
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
-
- Posts: 517
- Joined: May 4th, 2020, 3:01 pm
- Location: Ankara, TURKEY
Re: Another regexp question - how to get the folder name in a path?
But It works fine for me
You do not have the required permissions to view the files attached to this post.
I don't know where i going from here, but i promise it won't be boring...
-
- Rainmeter Sage
- Posts: 16183
- 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)...
-
- Posts: 1366
- Joined: September 7th, 2020, 2:24 pm
- Location: QLD, Australia
Re: Another regexp question - how to get the folder name in a path?
You do not have the required permissions to view the files attached to this post.
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.