Page 1 of 1

Regex to remove file name from path

Posted: Mon Aug 28, 2023 7:33 am
by horst.epp
How can I remove the file name from a complete file path.
The result should be the folder name.

Example:
A:\music\1966\The Who Sings My Generation\1. Out In The Street.mp3
to
A:\music\1966\The Who Sings My Generation\

Re: Regex to remove file name from path

Posted: Mon Aug 28, 2023 8:22 am
by void
You can use regex to match a pattern, not remove the name from a path.

Where are you using this regex search?



Everything has the preprocessor to remove the name part:

[pathpart:"A:\music\1966\The Who Sings My Generation\1. Out In The Street.mp3"]

[path-path:]

Re: Regex to remove file name from path

Posted: Mon Aug 28, 2023 8:23 am
by horst.epp
Found it using KI :D
Regex is
[^\\]*$

Usage is to use playlists as base for manipulating dirs in Total Commander.
Not my job, but some user asked for help.

I will remember path-part :)

Re: Regex to remove file name from path

Posted: Mon Aug 28, 2023 3:07 pm
by therube
Vim:
s/\(.*\\\).*/\1/


match anything up to the final \ (greedy)
-> .*\\
match everything else
-> .*
keep anything up to the final \
-> \1

(the "extra" \ are only there as ESC's)

playlists
(A long while back) I created a playlist & forgot to not include dirnames.
As it is my player, when hit with a direname, attempts to play all the files within that directory.
So the playlist itself listed file & directory names, & the player on hitting the directory, would then also play the files within, duplicating the already listed file names in the playlist itself.

Re: Regex to remove file name from path

Posted: Mon Aug 28, 2023 4:45 pm
by horst.epp
therube wrote: Mon Aug 28, 2023 3:07 pm Vim:
s/\(.*\\\).*/\1/
I find my version much simpler :)
Also, this Vim syntax doesn't work with any popular Windows Editor.
Tested with
AkelPad, Notepad++, PSPad