Replace string across multiple files and increment value
-
I’m looking for an urgent solution! We need to append a text string in 1800+ XML files and increment the value by 1. Alternatively, I could append the text string with the filename of the doc. Either solution would work.
Example 1
Original string is Benjamin_poster.jpg across all files
New value would be Benjamin_poster1.jpg, Benjamin_poster2.jpg etc.OR
Example 2
Original string is Benjamin_poster.jpg across all files
New value would be Benjamin_poster[filename1].jpg, Benjamin_poster[filename2].jpg etc.Any help would be great.
Thank you! -
$20 to the first correct submission ; )
-
Hello, Philip Lengden,
Oh, many thanks for your offer, but I could had help you, for free , just for the pleasure to give you a decent solution ;-))
Unfortunately, this kind of search / replacement can’t be easily achieved, even with the powerful regular expression engine,included in N++, because it needs some calculus, based on the
+
operation !But, don’t be sad ! This task should be easily run, in few lines of code, with a Python or Lua script, which can be performed, from inside Notepad++ ! However, up to now, I’m, personally, still unable to create such scripts !
So, Claudia, Scott and dail, here’s the solution to improve your Christmas’s holidays ! Some more beer ? An other gift ?.. I’m joking, of course :-))
Philip, I do hope that someone will help you, very soon !
Best regards,
guy038
-
Few questions:
Is there only one occurrence of the search string in each file or could it be multiple?
If there are multiple search strings in one file should all be replaced with same value
or should it be already incremented?
If only each file should get the incremented value, what if a file doesn’t have the search string,
can this incremented value be discarded or is it needed to assign it to the next file. Something likefile1 has the search string and will be replace with new_value1
file2 has the search string and will be replace with new_value2
file3 has not the search string
file4 has the search string and will be replaced with value (3 or 4)?Cheers
Claudia -
I have no doubt that with @Claudia-Frank on it, you will get more than a $20 solution. :-)
-
no pressure, no pressure - I thought of a simple solution like
import os search_string = 'HERE_YOUR_STRING' i = 0 for root, dirs, files in os.walk('C:\\WHAT_EVER_DIR\\'): # take care of double backslash like c:\\temp\\dir1\\ for file in files: fname, ext = os.path.splitext(file) if ext == '.xml': i+=1 full_path = os.path.join(root, file) with open(full_path) as f: s = f.read() s = s.replace(search_string, search_string + str(i)) with open(full_path, "w") as f: f.write(s)
but now, maybe an windows api solution?? ;-))
Cheers
Claudia