Edit Image Attributes in Multiple Files
I’m an amateur at coding, so forgive me if this question is silly or impossible or …
Anyhow - I have a few hundred XML files, most of which have at least one image. I need to add the attribute: “placement = break” to all of those images. Is there a way to (or a tool which) will search each XML file for images, and if the “placement” attribute is not present, add that attribute?
Or am I doomed to copy/paste purgatory for the next few weeks? ;)
This kind of task should be easily done with a Search/Replacement, in Regular expression mode ! And you could perform this S/R on multiple files, with the Find in Files N++ feature !
Just tell me the general syntax of your
imgtag, with its attributes and where the
placement="break"new attribute should be inserted.
See you later,
The image tag is super simple. The problem is that each of these XML files will have multiple images, all with different height & width attributes - which can’t be changed. That’s why I assumed that find/replace wouldn’t work.
How it is now:
<image href = “image1.jpg”
height = “203.950pt” width = “480.195pt” />
How I need it to be:
<image href = “image1.jpg”
height = “203.950pt” width = “480.195pt” placement = “break” />
Thanks for replying!
No problem ! We can, almost, do miracles, with regexes ! So :
- Copy this test text, below, in a new tab of Noteapd++ :
<image href = "image1.jpg" height = "203.950pt" width = "480.195pt" /> <image href = "image2.jpg" height = "107.123pt" width = "378.695pt" placement = "break" /> <image href = "image3.jpg" height = "999.001pt" width = "321,721pt" /> <image href = "image4.jpg" height = "27.123pt" width = "92.452pt" placement = "break" /> <image href = "image5.jpg" height = "654.987pt" width = "48,73pt" />
Remark : I supposed that two lines already contained the new attribute placement and I modified the different height and width values. Now :
Move back your cursor to the very beginning of this text
Open the Find/Replace dialog ( CTRL + H )
Select, first, the Regular expression search mode
In the Find what zone, type the regex
(?!.+placement)(<image.+?)(?= />)( Note the space before the closing tag
In the Replace with zone, type the regex
\1 placement = "break"( one space before the word placement )
Click on the Replace All button. At once, you’ll get the new text below :
<image href = "image1.jpg" height = "203.950pt" width = "480.195pt" placement = "break" /> <image href = "image2.jpg" height = "107.123pt" width = "378.695pt" placement = "break" /> <image href = "image3.jpg" height = "999.001pt" width = "321,721pt" placement = "break" /> <image href = "image4.jpg" height = "27.123pt" width = "92.452pt" placement = "break" /> <image href = "image5.jpg" height = "654.987pt" width = "48,73pt" placement = "break" />
As you can see, the placement attribute has been added, only on lines which did not contain this attribute, yet !
To be convinced, just try to repeat this S/R, by clicking, again, on the Replace All button
You should get the message : Replace All: 0 occurrences were replaced Logical !
If this S/R is the one that you’re looking for, I’ll explain you, next time, how this regex S/R work !
Well. I’m trying to reply with the results of my test and I’m getting “flagged as spam” when I try to submit.
Anyone help out with that?
Few days ago, the same misfortune happened to me:-(( As my posts are generally stored in simple .txt files, I just tried to re-post the first lines of that post. Then, little by little, I added 5/10 lines, each time, by editing the present contents of the post ( clicking on the vertical three dots symbol, on the right part of the screen ) and clicking on the Submit button, till I get, again, all the original post Strangely, although the contents seemed identical to my first ( spammed ) post, the second one was, this time, not spammed :-))
Anyway, you may also, if you prefer to, send me an e-mail, at my e-mail address :
The just of my reply was that this worked very well in the test run, but did not in my actual working files. Possibly because my image files aren’t actually named “image01, 02, 03” etc? Here are two actual strings:
(how very odd. it allowed this - but now I’m a “new user” and can only post once every twenty minutes. I might just email ya. ha)