loop through  a list of mxds (user input)

2749
4
01-29-2013 04:48 AM
JeromeWeis
New Contributor
Hello, everybody!

I have adapted a script that changes the data sources of the layers in one mxd provided by user input.
All I want is to have this script accept multiple mxds from user and loop through the script.
I know this situations has been described before, however I was not able to implement the advice found here.
I am at the very beginning of my python experience, therefore any help on this would be much, much appreciated.
I paste in the code with my trial for multiple mxd input:

import arcpy

#set new SDE connection
newSdePath = r"Database Connections\myConnection.sde"

#get mxds  as input from user
mxdAr = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument(mxdAr)

#parse through the list of mxds, dataframes, layers; change data sources

for mxdItem in mxd.split(";"):
        for df in arcpy.mapping.ListDataFrames(mxd):
                for lyr in arcpy.mapping.ListLayers(mxd, ""):
                        if lyr.isFeatureLayer== True:
                             lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
del mxd


The error i get :
File "D:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 608, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.


Thank you for your help!
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Jerome,

You could use the glob module to do this.  Here is an example:

import arcpy, glob

newSdePath = r"Database Connections\myConnection.sde"

for file in glob.glob(arcpy.GetParameterAsText(0) + r"\*.mxd"):
    mxd = arcpy.mapping.MapDocument(file)
    for df in arcpy.mapping.ListDataFrames(mxd):
        for lyr in arcpy.mapping.ListLayers(mxd, ""):
            if lyr.isFeatureLayer== True:
                lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)
    mxd.save()

del mxd


In the above example, set the data type for 'arcpy.GetParameterAsText' as 'Folder'.  The above code will then loop through the specified folder and update all MXDs.
0 Kudos
ChrisPedrezuela
Occasional Contributor III
This should solve the issue,

import arcpy

#set new SDE connection
newSdePath = r"Database Connections\myConnection.sde"

#get mxds as input from user
mxdAr = arcpy.GetParameterAsText(0).split(";")

#parse through the list of mxds, dataframes, layers; change data sources
for mxdItem in mxdAr:
    mxd = arcpy.mapping.MapDocument(mxdItem.strip("'"))
    for df in arcpy.mapping.ListDataFrames(mxd):
        for lyr in arcpy.mapping.ListLayers(mxd, ""):
            if lyr.isFeatureLayer== True:
            lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)
            arcpy.RefreshTOC()
            arcpy.RefreshActiveView()
    mxd.save()
del mxd


When you add the script as a tool, in its properties, the user input is set as Map Document and Multivalue.


Hello, everybody!

I have adapted a script that changes the data sources of the layers in one mxd provided by user input.
All I want is to have this script accept multiple mxds from user and loop through the script.
I know this situations has been described before, however I was not able to implement the advice found here.
I am at the very beginning of my python experience, therefore any help on this would be much, much appreciated.
I paste in the code with my trial for multiple mxd input:

import arcpy

#set new SDE connection
newSdePath = r"Database Connections\myConnection.sde"

#get mxds  as input from user
mxdAr = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument(mxdAr)

#parse through the list of mxds, dataframes, layers; change data sources

for mxdItem in mxd.split(";"):
        for df in arcpy.mapping.ListDataFrames(mxd):
                for lyr in arcpy.mapping.ListLayers(mxd, ""):
                        if lyr.isFeatureLayer== True:
                             lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
del mxd


The error i get :
File "D:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 608, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.


Thank you for your help!
0 Kudos
JeromeWeis
New Contributor
Thank you very much!
I learned something new.
It worked perfectly! It's not my victory...but feels good!

Cheers!



Hi Jerome,

You could use the glob module to do this.  Here is an example:

import arcpy, glob

newSdePath = r"Database Connections\myConnection.sde"

for file in glob.glob(arcpy.GetParameterAsText(0) + r"\*.mxd"):
    mxd = arcpy.mapping.MapDocument(file)
    for df in arcpy.mapping.ListDataFrames(mxd):
        for lyr in arcpy.mapping.ListLayers(mxd, ""):
            if lyr.isFeatureLayer== True:
                lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)
    mxd.save()

del mxd


In the above example, set the data type for 'arcpy.GetParameterAsText' as 'Folder'.  The above code will then loop through the specified folder and update all MXDs.
0 Kudos
JeromeWeis
New Contributor
Hi, there!
The same as above. Thank you very much!
It was so simple, but without you, I couldn't have done it. I learned something!
Much appreciated!
Cheers!

Jerome


This should solve the issue,

import arcpy

#set new SDE connection
newSdePath = r"Database Connections\myConnection.sde"

#get mxds as input from user
mxdAr = arcpy.GetParameterAsText(0).split(";")

#parse through the list of mxds, dataframes, layers; change data sources
for mxdItem in mxdAr:
    mxd = arcpy.mapping.MapDocument(mxdItem.strip("'"))
    for df in arcpy.mapping.ListDataFrames(mxd):
        for lyr in arcpy.mapping.ListLayers(mxd, ""):
            if lyr.isFeatureLayer== True:
            lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)
            arcpy.RefreshTOC()
            arcpy.RefreshActiveView()
    mxd.save()
del mxd


When you add the script as a tool, in its properties, the user input is set as Map Document and Multivalue.
0 Kudos