I made this happy little arcpy script to fix some data source problems:
print "Hi, I will fix find and replace file path sections on your behalf. \nGive me a moment to just count the number of MXDs I'll be looking at for you today..."
import os
import arcpy
mxdfiles = [os.path.join(d, x)
for d, dirs, files in os.walk(r"PATH")
for x in files if x.endswith(".mxd")]
print "\nOk, I'll be working through "+str(len(mxdfiles))+" MXDs. \nStarting this process now..."
for item in mxdfiles:
print "\nWorking on: "+item
mxd = arcpy.mapping.MapDocument(item)
mxd.findAndReplaceWorkspacePaths(r"PATH OLD1", r"PATH NEW")
mxd.findAndReplaceWorkspacePaths(r"PATH OLD2", r"PATH NEW")
mxd.findAndReplaceWorkspacePaths(r"PATH OLD3", r"PATH NEW")
mxd.findAndReplaceWorkspacePaths(r"PATH OLD4", r"PATH NEW")
mxd.findAndReplaceWorkspacePaths(r"PATH OLD5", r"PATH NEW2")
mxd.save()
del mxd
print "Completed "+str(mxdfiles.index(item)+1)+" maps so far."
print "\nProcess complete!"
Unfortunately, though, when it comes across a MXD with a Bing imagery layer, up pops a Bing Authorization box which you have to click 'OK' on (hitting Enter works, too). This is because my company no longer has a license to use Bing/Microsoft Virtual Earth, so this little box comes up every time a MXD is opened manually, or, it seems, when one is invoked by my script. This means I have to either:
1. Find a way to programmatically click that button or ignore it.
2. Leave a coffee mug on my Enter key overnight.
3. Remove the Bing (or Microsoft Virtual Earth*) layers programmatically.
I Googled to no avail regarding option 1. Before going for option 2, I tried option 3. As a test, I used the following script to try removing Microsoft Virtual Earth* layers (their group is Microsoft Virtual Earth, inside that there are three layers with the same name plus Hybrid, Aerial or Roads suffixed):
import arcpy
mxd = arcpy.mapping.MapDocument(r"PATH.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd, "Microsoft Virtual Earth", df):
arcpy.mapping.RemoveLayer(df, lyr)
mxd.saveACopy(r"PATH2.mxd")
del mxd
It ran without errors, including without the Bing Authorization dialog box popping up. The resulting MXD, however, would not open without crashing. I therefore used this script to retrieve its list of layers:
import arcpy
mxd = arcpy.mapping.MapDocument(r"PATH2.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
print arcpy.mapping.ListLayers(mxd, "", df)
del mxd
...and found that the Microsoft Virtual Earth* layers had in fact been removed. Btw, the list layers script also did not cause the Bing Authorization box to pop up.
Before I try to incorporate my script to remove these layers into the one I'm working on to walk through all MXDs and fix their paths, I need to know:
1. What is causing the Bing Authorisation dialog box to pop up with my initial script, but not the layer deletion nor listing ones?
2. Why after running the Bing/Microsoft Virtual Earth layer deletion script the MXD is now corrupted and causes ArcMap to crash (crash report box comes up)?