MXD re-setting the links help please!

476
2
08-22-2012 08:33 AM
Z__NahideAydin
New Contributor II
Hi,

I have python script to resetting the links for mxds. But could not get it to work.

Please help!

import arcpy, os
path = "Z:\Maps"
for root, dirs, files in os.walk(path):
for fileName in files:
  basename, extension = os.path.splitext(fileName)
  if extension == ".mxd":
   fullPath = os.path.join(root, fileName)
   mxd = arcpy.mapping.MapDocumnet(fullPath)
   mxd.findAndReplaceWorkspacePaths("Z:\maps\xxx.mdb", "Z:\maps\cultural\xxx.mdb")
   mxd.saveACopy("Z:\maps\cultural\repaired_links")
   print mxd.dateSaved

Thanks in advance!
Tags (2)
0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
2 immediate errors I see are:

1) MapDocument was spelled incorrectly
2) You are not specifying an output MXD name with .saveACopy

Also - when using backslashes in a string it is best to place an "r" (for raw) in front of the string to avoid possible escape characters.

I made all corrections below

import arcpy, os
path = r"Z:\maps"
for root, dirs, files in os.walk(path):
  for fileName in files:
    basename, extension = os.path.splitext(fileName)
    if extension == ".mxd":
       fullPath = os.path.join(root, fileName)
       mxd = arcpy.mapping.MapDocument(fullPath)
       mxd.findAndReplaceWorkspacePaths(r"Z:\maps\xxx.mdb", r"Z:\maps\cultural\xxx.mdb")
       newPath=os.path.join(r"Z:\maps\cultural\repaired_links", fileName)
       mxd.saveACopy(newPath)
       print mxd.dateSaved


Jeff
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I would also recommend saving to a path outside of where you are using the 'os.walk' function.  Once the new MXD is saved, the script will try and update the paths of that MXD, and even though paths do not need to be updated, it will error due to a lock on the MXD from the current script.
0 Kudos