0

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.

Celt
  • 2,469
  • 2
  • 26
  • 43
skavan
  • 417
  • 1
  • 9
  • 17

2 Answers2

0

-- Part 1: generic replacement --

You could give the following regular expression a try:

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

To break it down:

  • 2 forward slashes
  • Followed by one or more characters (. matches anything, + means 1 or more)
  • Followed by a slash
  • Followed by anything that is not a slash: the filename before the extension ([^\] means anything except of slash, + once again means one or more).
  • Followed by a dot (\. escapes the dot, so that is interpreted literally)
  • Followed by jpg or png (| means OR)

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.

chocochaos
  • 1,466
  • 10
  • 15
  • Gosh - that's fab...the Find Regex works really well - but I was unclear what I need on the replace side. Let's say the file I am editing (the xml file) is in path \\SERVER\Dir1\. I need to append the $1 to this, So that the replace string is \\SERVER\Dir1\filename.ext. So somehow I need to get the path to file I am editing...is this possible? – skavan Jul 15 '15 at 16:58
  • I suppose you know the path of the file you are currently replacing text in? Or are you trying to do it for multiple files at the same time? – chocochaos Jul 16 '15 at 08:17
  • Never mind, I just read the original question again. There's no way to do it in one single action I think, but you can do it with a macro. I will update my answer in a minute. – chocochaos Jul 16 '15 at 11:09
  • I have updated my answer. You could of course also combine both solutions into 1 macro that you execute for each file, whatever you prefer. I think this should give you enough guidance to be able to do it though :) – chocochaos Jul 16 '15 at 11:22
0

Here's a python script version:

import os.path
def replace_func(m):
try:
    head, tail = os.path.split(m.group(0))
    tail = "\\" + tail
    newname = os.path.dirname(notepad.getCurrentFilename()) + tail
    newname = newname.replace("\\","/")
    return newname
except:
    return m.group(0)

editor.rereplace('//.*/([^/]+\.(jpg|png))', replace_func)
skavan
  • 417
  • 1
  • 9
  • 17