We have a new database connection and all our MXDs have to be updated. I ran a script to update the layers on a bunch of MXDs. Then I opened a map, right clicked a layer, properties, then checked under the source tab to see if the changes took place. But, the layer was still referencing the old connection. Is there a snippet I could include in this script, or something else, that verifies that the layers have indeed been updated?
There is a print statement in the script, but that just prints the name of the MXD. It doesn't really give concrete proof.
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">find_and_replace</SPAN><SPAN class="punctuation token">(</SPAN>MXD_workspace<SPAN class="punctuation token">,</SPAN> oldPath<SPAN class="punctuation token">,</SPAN> newPath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> MXD_workspace
path <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace
<SPAN class="keyword token">for</SPAN> root<SPAN class="punctuation token">,</SPAN> dirs<SPAN class="punctuation token">,</SPAN> files <SPAN class="keyword token">in</SPAN> os<SPAN class="punctuation token">.</SPAN>walk<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> files<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> name<SPAN class="punctuation token">.</SPAN>endswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">".mxd"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
mxd_name <SPAN class="operator token">=</SPAN> name
fullpath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN>name<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> mxd_name
<SPAN class="keyword token">print</SPAN> fullpath
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>fullpath<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> df <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> flayer <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">,</SPAN> df<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> flayer<SPAN class="punctuation token">.</SPAN>isFeatureLayer <SPAN class="operator token">or</SPAN> flayer<SPAN class="punctuation token">.</SPAN>isRasterLayer<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>findAndReplaceWorkspacePaths<SPAN class="punctuation token">(</SPAN>oldPath<SPAN class="punctuation token">,</SPAN> newPath<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Repaired the path for "</SPAN> <SPAN class="operator token">+</SPAN> name
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>mxd_name <SPAN class="operator token">+</SPAN> <SPAN class="string token">" cannot replace paths"</SPAN><SPAN class="punctuation token">)</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> mxd
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"complete..."</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
find_and_replace<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_18\CountyBoard"</SPAN><SPAN class="punctuation token">,</SPAN>
r<SPAN class="string token">"Database Connections\BonnScott.sde"</SPAN><SPAN class="punctuation token">,</SPAN>
r<SPAN class="string token">"Database Connections\gisEddy.gissql.sde"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#(path2MXDs, path2oldGDB, path2newGDB)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>