Notepad++ – How to remove everything after the last backslash “\” on a line in a text file

notepadregex

I have a list of about 30,000 files (with paths) that need to be individually copied along with their folder structures. Unfortunately, the paths are very long and the file copy utilities that will handle this function require that I specify destination folders for each file, which means I need to remove the file name from each of the 30,000 lines, leaving the folder structure intact.

What I need to do is find a way through a regular expression replace in Notepad++ that will delete all of the data on each line of the document after the last backslash "\" on the line.

Example:

C:\Data\ToCopy\file1.txt
C:\Data\ToCopy\Folder1\file2.txt
C:\Data\ToCopy\Folder2\file3.txt
C:\Data\ToCopy\Folder2\Subfolder1\file4.txt

Desired Output:

C:\Data\ToCopy
C:\Data\ToCopy\Folder1
C:\Data\ToCopy\Folder2
C:\Data\ToCopy\Folder2\Subfolder1

Any ideas on how to format the expression to allow this? I've tried [^"\"]+$, but that appears to be invalid.

Best Answer

You can use a RegEx capture group (denoted by the round brackets) to capture all characters (.*) up to (and including) the slash (\\ - you need to 'escape' the slash with another slash), and also include the rest of the characters in the Regex match (the second .*), since we want to replace them as well.

Then Replace the whole line with the contents of (just) that capture group (\1).

So this works for me:

Find: (.*\\).*

Replace with: \1

Make sure ". matches new line" is disabled.

Related Question