In this article you will learn how to re-establish missing databases within various MXD files using Python.
The loss of link between ArcMap files and spatial databases occurs for one of these reasons:
How to repair paths in an ArcGIS .MXD file?
1) Open the MXD file.
2) Go to the layer with the broken link, double right click on it, click on the Data item and click on the “Repair Data Source...” button.
3) Find the Shapefile / Feature Class that corresponds to the layer in the new location.
4) Click on accept.
5) Save MXD file.
7) Optional step: If the information was stored in different databases or folders, it will be necessary to repeat the procedure for each layer or layers with different data sources.
If this procedure has to be repeated with each of the layers within the MXD file that have lost their data source, which can be quite time consuming; if in addition to several layers, there are several MXD's with this problem, this would become a colossal time expenditure. To save you all that time, I will show you how it can be much more efficient using Python code (don't worry, you don't need to know how to program to do it).
How to repair MXD (ArcMap) file data sources automatically?
Step 1: Open a blank ArcGIS file.
Step 2: Open the Python window in the Standard toolbar.
Step 3: Backup the original MXD files.
Step 4: Identify the missing paths and the current paths to the data. To check the missing paths, right click on the layer (a), press the “Properties...” button (b) and go to the “Source” tab (c), in the “Location:” item (d) you will find the path where the spatial data was previously stored.
Step 5: Copy the following code into a notepad and replace the text C:\EXAMPLE\MXD in line #02 with the path to the folder where you have stored the MXD files with the problem.
Step 6: In line #06 of the code replace the path C:\EXAMPLE\OLD_FOLDER with the previous and/or missing path identified in step 4.
Step 7: In the same line #06 of the code replace the path C:\EXAMPLE\NEW_FOLDER with the new path where the spatial information is stored.
Step 8: Copy the modified code into the Python window and press the Enter key twice.
After a few seconds you will see how the modification date of the ArcMap files starts to be updated, and when you open it you will notice the recovery of the link in the layers that presented this problem.
Notes:
- You can repeat line #06 as many times as you want in case you need to modify and/or repair more than one missing route at a time. This copy must be made exactly below line #06 and would become line #07 so that the code would have ten lines, and so on with each path to be repaired.
I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.
BONUS
In future occasions when a new project with geographic information systems using ArcMap try to do the following with all your MXD files to avoid that when moving the complete folder of the project you lose the sources of information in your maps and/or graphical outputs (Case D).




