Upgrading an Old Python Script : Change all Image Paths at once

I found those script i Think it was for Blender 2.49 to change all imagePath at once,
but I cant get it to work in Blender 2.8...

Can some one help , it would be very handie for flies with lots of images?

import bpy  #Original import Blender

from bpy import context, data, ops
#from ORIGINAL from Blender import Texture, Image, Material

#insert the path which should be replaced
oldpath = "C:\\OLDpath\\"

#insert the new path which replaces the old one
newpath = "D:\\NewPth\\"

image_count = 0

for tex in bpy.data.textures.get():

image = tex.getImage()
#print image.name

filename = ""

if (tex.getType() == "Image"):

if (len(image.getFilename()) > 0):

i=0
while i < len(image.getFilename()) :

if image.getFilename()[i:i+1] == "\\" :
filename += "/"
else :
filename += image.getFilename()[i:i+1]

i+=1

if (oldpath == filename[0:len(oldpath)]):

temp = filename[len(oldpath):len(filename)]
temp = newpath + temp

image.filename = temp

image_count += 1

print ("image path's changed:")
print (image_count)


I did a few changes, but cant find the rest od my errors. I thing some stuff is now depricated but I cant find the 2.8 Version

here is the Path to the ORIGINAL Script https://blenderartists.org/t/script-to-change-paths-for-all-images/460733

You are right, this script is severely outdated (2009 !) This should do :

import bpy

# The path which should be replaced
oldpath = "C:\\OLDpath\\"

# The new path which replaces the old one
newpath = "D:\\NewPth\\"

image_count = 0

for image in bpy.data.images:
if not image.filepath.startswith(oldpath):
continue
image.filepath = image.filepath.replace(oldpath, newpath)

We take advantage of the python string method string.replace(old_string, new_string) to swap the paths. Note that they must both be written the same way, like in your example.
• Or could for image in bpy.data.images: to change all image filepaths rather than only those assoc with an image texture. Q suggests this a bit, old script goes over textures? Wonder if it's worth mentioning relative paths, in that, for example: as long as a folder named images is in same folder as blend file then //images/foo.png will be path to image relative to blend file. Was on BA answering Qs back then. 2009 would have been a 2.49 hanger on... the change from 2.7 to 2.8 was nothing like the upheaval of 49b to 50 Jun 17 '20 at 11:01
• for sure more in line with handy to use relative paths if you are moving folders around to avoid need to re-path. Another small thing consider avoiding reloading images with unchanged paths... filepath.startswith(oldpath) and report updated 3 of 10 (eg). Jun 17 '20 at 11:25