Regex to remove file name from path

Off-topic posts of interest to the "Everything" community.
Post Reply
horst.epp
Posts: 1456
Joined: Fri Apr 04, 2014 3:24 pm

Regex to remove file name from path

Post 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\
void
Developer
Posts: 17149
Joined: Fri Oct 16, 2009 11:31 pm

Re: Regex to remove file name from path

Post 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:]
horst.epp
Posts: 1456
Joined: Fri Apr 04, 2014 3:24 pm

Re: Regex to remove file name from path

Post 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 :)
therube
Posts: 5056
Joined: Thu Sep 03, 2009 6:48 pm

Re: Regex to remove file name from path

Post 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.
horst.epp
Posts: 1456
Joined: Fri Apr 04, 2014 3:24 pm

Re: Regex to remove file name from path

Post 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
Post Reply