English

How To: Set relative paths for all map documents in a folder using Python

Summary

The map document properties provides the option to set relative paths for layers within the map document. The paths are stored as incomplete paths that are relative to the current location of the map document.

A relative path is distinguished from and absolute path by referencing a resource with relation to the object in question. For example, consider the directions to a person's house. A street address is an absolute path; there is no ambiguity in 123 First Street, Anytown, USA 12345. However, one might reference a relative path to the same address from a known position, such as, second left, third house on the right.

Similarly, we can reference paths on a computer drive using the command prompt: C:\myFiles\pictures and C:\myFiles\text are absolute references, however the relative path from C:\myFiles\text to C:\myFiles\pictures may be referenced as ..\pictures, that is, up one folder level from text to myFiles, and down to the pictures subfolder.

Procedure

Relative paths can be set by opening the properties of a map document and choosing the option to store the relative paths of the data sources. This process can be time consuming if done one document at a time. However, the following Python script automates this process for all map documents within a folder.

import arcpy, os

#workspace to search for MXDs
Workspace = r"c:\Temp\MXDs"
arcpy.env.workspace = Workspace

#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")

#set relative path setting for each MXD in list.
for file in mxdList:
    #set map document to change
    filePath = os.path.join(Workspace, file)
    mxd = arcpy.mapping.MapDocument(filePath)
    #set relative paths property
    mxd.relativePaths = True
    #save map doucment change
    mxd.save()
Note:
If it is desired to have all new maps stored with relative paths, it is necessary to specify relative paths as the default. Click Customize > ArcMap Options to open the ArcMap Options dialog box. Click the General tab. Check the option to 'Make relative paths the default for new map documents'.

Related Information