I have a folder and would like to iterate through each MXD and take each SDE feature class that is in it and place in a new folder and file geodatabase, with a new MXD using the file geodatabase paths.
- folder with original MXD name
- new MXD in new folder with original MXD name
- new file geodatabase with all feature classes from MXD
Each new MXD should have the above associated with it.
I have a solid understanding of each function that I would like to write aside from iterating and copying MXDs and their features, how should this be done with arcpy?
This script iterates through a folder and all MXDs and prints the MXD and feature classes used in the MXD. I would like to further this by accessing each feature class from arcpy.mapping
and sending those to a file geodatabase. And making a new MXD with the new feature classes.
import arcpy, os, datetime
folderPath = 'C:\MXD_test'
#Loop through each MXD file
for root, dirs, files in os.walk(folderPath):
for file in files: # files is a list of files in the current directory
if file.lower().endswith(".mxd"):
fullpath = os.path.join(root, file) # root is the current directory
#Reference MXD
mxd = arcpy.mapping.MapDocument(fullpath)
DFList = arcpy.mapping.ListDataFrames(mxd)
for df in DFList:
# Format output values
if df.description == "":
descValue = "None"
else:
descValue = df.description
# ==== Note the new descName variable
descName = df.name
lyrList = arcpy.mapping.ListLayers(mxd, "", df)
for lyr in lyrList:
lyrName = lyr.name
if lyr.supports("dataSource"):
lyrDatasource = lyr.dataSource
else:
lyrDatasource = "N/A"
print lyrDatasource
print fullpath
Best Answer
This update to your script creates new folder and new file geodatabase in each folder. As @Vince mentions in a comment above, if different MXDs are pointing to the same large SDE datasets you'll be copying the same data multiple times. An alternative would be to create a single File Geodatabase to store all the data in.
This is the basic process the script follows:
Things to watch out for:
os.walk
will find the new MXDs if the output location is within a subfolder of your folderPath. My script above has assumed this is not the case, but could be worked around if necessaryFieldName
becomes[FieldName]
etc. This can probably be worked around if required.