1

I'm very new to python so please forgive me if this is a simple error. I have quite a few MXD's in which each have a few layers referencing the same source. However I have since moved this data to a new folder. I have written a script to automatically refresh the similar layers.

The script runs through, however when I check to see if its worked, none of the layers are refreshed. I'm not sure whats wrong.

import arcpy, os

#workspace to search for MXDs
Workspace = r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\MXD"
arcpy.env.workspace = Workspace

#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")
print mxdList

#set new link for specific shapefile in each MXD in list.
for file in mxdList:
#set map document to change
    filePath = os.path.join(Workspace, file)
    mxd = arcpy.mapping.MapDocument(filePath)

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        if lyr.dataSource == r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1\MMO_South_Plan_Areas.shp":
            lyr.findAndReplaceWorkspacePath(r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1", r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\SHP")
    if lyr.dataSource == r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1\Land.shp":
            lyr.findAndReplaceWorkspacePath(r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1", r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\SHP")

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
print "Success"
6
  • Both of your for loops look like they would throw unexpected indentation errors - have you tried running precisely the code that you have posted?
    – PolyGeo
    May 12, 2015 at 9:52
  • Hi. Sorry, the code was not pasted correctly. The 'for' loops have been formatted correctly, with the indentation being in the correct place. I have edited the above to reflect the correct formatting.
    – MacroZED
    May 12, 2015 at 9:57
  • Please edit your question to reflect the code that you are actually running. I think you should include some print statements within the code blocks for each test that you perform so that you will know whether or not they are being run. That print output should accompanying the code to help us interpret it too.
    – PolyGeo
    May 12, 2015 at 10:00
  • 1
    If that is your actual code, your for lyr in arcpy.mapping.ListLayers(mxd): loop will only run on the final mxd as it is not indented to be inside the for file in mxdList: loop.
    – user2856
    May 12, 2015 at 10:03
  • How do I add it to the ' for file in mxdList: ' loop?
    – MacroZED
    May 12, 2015 at 10:19

1 Answer 1

2

As commented by @Luke, and as we were both working towards:

If that is your actual code, your for lyr in arcpy.mapping.ListLayers(mxd): loop will only run on the final mxd as it is not indented to be inside the for file in mxdList: loop.

Instead of your code try:

import arcpy, os

#workspace to search for MXDs
Workspace = r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\MXD"
arcpy.env.workspace = Workspace

#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")
print mxdList

#set new link for specific shapefile in each MXD in list.
for file in mxdList:
#set map document to change
    filePath = os.path.join(Workspace, file)
    mxd = arcpy.mapping.MapDocument(filePath)

    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource == r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1\MMO_South_Plan_Areas.shp":
                lyr.findAndReplaceWorkspacePath(r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1", r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\SHP")
        if lyr.dataSource == r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1\Land.shp":
                lyr.findAndReplaceWorkspacePath(r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1", r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\SHP")

    mxd.save()
    print "Success"

I removed the arcpy.RefreshActiveView() and arcpy.RefreshTOC() because they only do something when your MapDocument object has been created using "CURRENT".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.