Page 1 of 3

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

Posted: August 28th, 2022, 6:21 pm
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.

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

Posted: August 28th, 2022, 6:45 pm
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.

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

Posted: August 28th, 2022, 7:15 pm
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?

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

Posted: August 28th, 2022, 7:27 pm
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"

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

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

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

Posted: August 28th, 2022, 7:32 pm
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:

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

Posted: August 28th, 2022, 7:44 pm
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\"

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

Posted: August 28th, 2022, 7:47 pm
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

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

Posted: August 28th, 2022, 7:48 pm
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)...

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

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

Untitled.png
Capture1.PNG
Capture1.PNG