3

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.

  • 3
    \\[^\\]*$ is not doing the trick ? – dvhh Dec 18 '14 at 2:42
  • If you give more details about copies you are going to make, maybe somebody can help with Robocopy, it's got a lot of functions. – SΛLVΘ Nov 6 '15 at 10:26
1

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.

0

I answered my own question, hope this helps someone else trying to robocopy a specific list of files to their original folder structure. Edit the list in Notepad++ and use the replace function (select 'Regular Expression') and use the following string:

\\(?<=\\)[^\\]+$

Replace that with blank and you're good to go.

  • Can you expand on what that RegEx does exactly? – Ƭᴇcʜιᴇ007 Dec 16 '14 at 21:36
  • 2
    Why the lookbehind? Try this \\[^\\]+$ – nixda Dec 16 '14 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.