Select to view content in your preferred language

Change path for every mxd in a folder

3052
3
Jump to solution
06-18-2013 09:02 AM
JanBenson
Deactivated User
Our office uses roaming profiles and redirected folders.  This means the location of each file in an mxd has the domain name in its path (\\xxxx.xxxx.xxx\...).  We just changed our domain from \\xxxx.xxxx.xxx\... to \\x2x2x2x2.x2x2x2x2x2\... where ... is the rest of the path that has not changed.  I tried to use python code to change the path of every mxd in the directory without success. I???ve had some help  as I am not a python programmer.  The first set of code gives an error message, Runtime Error! Program C:\PYTHON26\ARCGIS10.1\pythonw.exe abnormal program termination.  The second code completes, but does not change the path.  It does update the Date modified.  The print file statements were in for debugging.  What do I need to change to have the paths changed?  Surely someone has had to do this before.  FYI, I am using 10.1.  Thanks!

First code:
import arcpy, os, sys  # Folder location of maps that need updating. Setup for script tool. Can be changed to hardcoded path. folderpath = arcpy.GetParameterAsText(0)  arcpy.env.workspace = r"\\x2x2x2x2.x2x2x2x2x2\???-ocd\Users\my.name\My Documents" for file in arcpy.ListFiles("*.mxd"):     print file     mxd = arcpy.mapping.MapDocument(os.path.join(folderpath,file))     mxd.findAndReplaceWorkspacePaths(r"\\xxxx.xxxx.xxx", r"\\x2x2x2x2.x2x2x2x2x2")     mxd.save()     del mxd sys.exit()


Second code:
import arcpy, os, sys  # Folder location of maps that need updating. Setup for script tool. Can be changed to hardcoded path. folderpath = arcpy.GetParameterAsText(0)  arcpy.env.workspace = "\\\\nmfs.local\\akc-ocd\\Users\\jan.benson\\My Documents\\" for file in arcpy.ListFiles("*.mxd"):     print file     mxd = arcpy.mapping.MapDocument(os.path.join(folderpath,file))     print file     oldpath = "\\\\xxxx.xxxx.xxx\\???-ocd\\Users\\my.name\\My Documents\\"     newpath = "\\\\x2x2x2x2.x2x2x2x2x2\\???-ocd\\Users\\my.name\\My Documents\\"     mxd.findAndReplaceWorkspacePaths(oldpath, newpath)     print file     mxd.save()     del mxd
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Jan,

I believe the problem is with the arcpy.mapping.MapDocument function.  After performing some tests, it doesn't seem like you can use a UNC path with this function.  For example, if I run the following code the MXDs will not be updated:

import arcpy, os, sys  folderpath = r"\\server\temp\python"  arcpy.env.workspace = r"\\server\temp\python" for file in arcpy.ListFiles("*.mxd"):     mxd = arcpy.mapping.MapDocument(os.path.join(folderpath,file))     oldpath = r"C:\Temp\Python\Test.gdb"     newpath = r"\\server\Temp\Python\Test.gdb"     mxd.findAndReplaceWorkspacePaths(oldpath, newpath)     mxd.save()     del mxd


However, if I replace the 'folderpath' variable with an absolute path, the code will update the MXDs successfully:

import arcpy, os, sys  folderpath = r"C:\temp\python"  arcpy.env.workspace = r"\\server\temp\python" for file in arcpy.ListFiles("*.mxd"):     mxd = arcpy.mapping.MapDocument(os.path.join(folderpath,file))     oldpath = r"C:\Temp\Python\Test.gdb"     newpath = r"\\server\Temp\Python\Test.gdb"     mxd.findAndReplaceWorkspacePaths(oldpath, newpath)     mxd.save()     del mxd


I'm not sure if this is by design, or if this is a bug.  May be worth logging an incident with tech support.  Are you able to use an absolute path rather than a UNC path for the folderpath variable?

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Jan,

I believe the problem is with the arcpy.mapping.MapDocument function.  After performing some tests, it doesn't seem like you can use a UNC path with this function.  For example, if I run the following code the MXDs will not be updated:

import arcpy, os, sys  folderpath = r"\\server\temp\python"  arcpy.env.workspace = r"\\server\temp\python" for file in arcpy.ListFiles("*.mxd"):     mxd = arcpy.mapping.MapDocument(os.path.join(folderpath,file))     oldpath = r"C:\Temp\Python\Test.gdb"     newpath = r"\\server\Temp\Python\Test.gdb"     mxd.findAndReplaceWorkspacePaths(oldpath, newpath)     mxd.save()     del mxd


However, if I replace the 'folderpath' variable with an absolute path, the code will update the MXDs successfully:

import arcpy, os, sys  folderpath = r"C:\temp\python"  arcpy.env.workspace = r"\\server\temp\python" for file in arcpy.ListFiles("*.mxd"):     mxd = arcpy.mapping.MapDocument(os.path.join(folderpath,file))     oldpath = r"C:\Temp\Python\Test.gdb"     newpath = r"\\server\Temp\Python\Test.gdb"     mxd.findAndReplaceWorkspacePaths(oldpath, newpath)     mxd.save()     del mxd


I'm not sure if this is by design, or if this is a bug.  May be worth logging an incident with tech support.  Are you able to use an absolute path rather than a UNC path for the folderpath variable?
0 Kudos
Kathleen_Crombez
Frequent Contributor

I am running into the same problem using findAndReplaceWorkspacePaths not being saved after calling mxd.save()

Except all of my paths are absolute paths. No UNC paths being used for map document or connection files.

0 Kudos
JanBenson
Deactivated User
If I map the path to a drive, I could use a letter.  However we are instructed to use UNC naming convention.  The bigger problem is that our old domain no longer exists so that it cannot be mapped.  Thanks very much for your help.  I will follow up with technical support to see if it is a bug or whether it isdesigned as is.

Thanks again,
Jan
0 Kudos