It is currently February 4th, 2023, 3:34 am

Another regexp question - how to get the folder name in a path?

Get help with creating, editing & fixing problems with skins
User avatar
CodeCode
Posts: 1111
Joined: September 7th, 2020, 2:24 pm
Location: QLD, Australia

Another regexp question - how to get the folder name in a path?

Post by CodeCode »

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.
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
User avatar
balala
Rainmeter Sage
Posts: 14935
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Another regexp question - how to get the folder name in a path?

Post by balala »

CodeCode wrote: August 28th, 2022, 6:21 pm How to use regexp to get the folder from a path of indeterminate length?
For instance:

Code: Select all

[Variables]
File=c:\Program Files\Rainmeter\Rainmeter.exe

[MeasureFolder]
Measure=String
String=#File#
RegExpSubstitute=1
Substitute="^(.*)\\(.*)$":"\1"
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.
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.
User avatar
CodeCode
Posts: 1111
Joined: September 7th, 2020, 2:24 pm
Location: QLD, Australia

Re: Another regexp question - how to get the folder name in a path?

Post by CodeCode »

balala wrote: August 28th, 2022, 6:45 pm For instance:

Code: Select all

[Variables]
File=c:\Program Files\Rainmeter\Rainmeter.exe

[MeasureFolder]
Measure=String
String=#File#
RegExpSubstitute=1
Substitute="^(.*)\\(.*)$":"\1"
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.
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.
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?
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
User avatar
tass_co
Posts: 433
Joined: May 4th, 2020, 3:01 pm
Location: Ankara, TURKEY

Re: Another regexp question - how to get the folder name in a path?

Post by tass_co »

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... :great:
User avatar
CodeCode
Posts: 1111
Joined: September 7th, 2020, 2:24 pm
Location: QLD, Australia

Re: Another regexp question - how to get the folder name in a path?

Post by CodeCode »

tass_co wrote: August 28th, 2022, 7:27 pm

Code: Select all

Substitute="^.*\\(.*)\.*$":"\1"
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.
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
User avatar
tass_co
Posts: 433
Joined: May 4th, 2020, 3:01 pm
Location: Ankara, TURKEY

Re: Another regexp question - how to get the folder name in a path?

Post by tass_co »

CodeCode wrote: August 28th, 2022, 7:30 pm 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.

Code: Select all

Substitute="^.*\\(.*)\.*$":"\1\"
:thumbup:
I don't know where i going from here, but i promise it won't be boring... :great:
User avatar
CodeCode
Posts: 1111
Joined: September 7th, 2020, 2:24 pm
Location: QLD, Australia

Re: Another regexp question - how to get the folder name in a path?

Post by CodeCode »

tass_co wrote: August 28th, 2022, 7:32 pm

Code: Select all

Substitute="^.*\\(.*)\.*$":"\1\"
:thumbup:
Sorry to be a pain. but that returns "\1\"
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
User avatar
tass_co
Posts: 433
Joined: May 4th, 2020, 3:01 pm
Location: Ankara, TURKEY

Re: Another regexp question - how to get the folder name in a path?

Post by tass_co »

CodeCode wrote: August 28th, 2022, 7:44 pm Sorry to be a pain. but that returns "\1\"
But It works fine for me :???:
Untitled.png
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... :great:
User avatar
balala
Rainmeter Sage
Posts: 14935
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Another regexp question - how to get the folder name in a path?

Post by balala »

CodeCode wrote: August 28th, 2022, 7:15 pm 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?
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)...
User avatar
CodeCode
Posts: 1111
Joined: September 7th, 2020, 2:24 pm
Location: QLD, Australia

Re: Another regexp question - how to get the folder name in a path?

Post by CodeCode »

tass_co wrote: August 28th, 2022, 7:47 pm But It works fine for me :???:

Untitled.png
Capture1.PNG
Capture1.PNG
You do not have the required permissions to view the files attached to this post.
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.