regexnotepad++nppexec

Notepad++ search for path reference and replace with the file's path?


I am using notepad++ and I have a number of xml files. For example, let's say the XML file is SomeXML.XML.

Within the file, there will be entries such as: //SERVER-NAME/Graphics/Materials/Downloaded/Fabric Grey.jpg

I wish to find these entries (they all start with \SERVER-NAME and end with jpg or png) and replace them with:

//The path to the SomeXML.XML/Fabric Grey.jpg

It must be doable - but I can't figure it out! H-E-L-P.


Solution

  • -- Part 1: generic replacement --

    You could give the following regular expression a try:

    //.*/([^/]+\.(jpg|png))
    

    To break it down:

    Then just replace with whatever you like. If you use $1 in the replacement it will be replaced with the filename. So in your example SomeXML.XML/$1 would be replaced with SomeXML.XML/Fabric Grey.jpg.

    Screenshot of the replace window in notepad++

    -- Part 2: replacing SomeXML.XML with the current filename --

    Unfortunately putting in the filename can not be done in the same replace action. It has to be done for each file separately, but a macro can help speed it up. Note that the steps below include recording said macro, so it is vital they are executed exactly as described.

    1. Open the files in notepad++ (having executed the above replacements first).
    2. Click Macro -> Start Recording.
    3. Press ctrl+f to open the find window.
    4. Go the the replace-tab.
    5. In the find-box, but SomeXML.XML.
    6. Empty the replace with box.
    7. Click Edit -> Copy to Clipboard -> Current Filename to Clipboard.
    8. Press ctrl+v in the replace with box to paste the filename.
    9. Click Replace All (NOT in all opened documents).
    10. Close the replace-window.
    11. Click Macro -> Stop Recording.
    12. Now in every file where you want to do the replacements, press Ctrl+Shift+P to execute the recorded macro.

    Not fully automated, but this should already make your life quite a bit easier.