in real life find and replace file duplicates is not simple thing. you have to check a lot of parameters and input and output connections. but in your probably case the difference is only texture file, and connection to change is outColor.
so the script might be:
import maya.cmds as cmds
import os
## check if paths are equal
def same_path(file1, file2):
return (os.path.abspath(file1.lower()) == os.path.abspath(file2.lower()))
## check if two files use same textures
def uses_same_file(file1, file2):
f1 = cmds.getAttr(file1 + ".fileTextureName")
f2 = cmds.getAttr(file2 + ".fileTextureName")
return same_path(f1, f2)
## reconnect out color of file1 to destination connection of out color file2
def reconnect_file_outcolor(file1, file2):
dest = cmds.listConnections(file2 + ".outColor", s=1, p=1)
if dest:
curr = cmds.listConnections(file1 + ".outColor", s=1, p=1)
for d in dest:
if d not in curr: cmds.connectAttr(file1 + ".outColor", d, force=1)
return len(dest)
## search all duplicates and return two lists >> targets(to keep) and sources(for delete)
def replace_same_files():
files = set(cmds.ls(type = "file"))
targets = []
sources = []
while files:
target = files.pop()
for source in files:
if uses_same_file(target, source):
reconnect_file_outcolor(target, source)
sources.append(source)
targets.append(target)
files = files.difference(sources)
return targets, sources
replace_same_files()
(not really tested well. let me know if anything works wrong)