Moving Data between different network drives. How do I fix broken file paths?

1123
7
Jump to solution
08-02-2012 02:03 AM
IanCrabb
New Contributor
Hi,

I have quite a lot of data (layers/Shps/mxd???s) saved in different places across two different network drives. Is there a way that I could just drag everything into one folder on one drive and run some sort of python script that will tell ArcGIS desktop10.1 to ignore the full file path of the layers and only look for the latter half of the file path in the specific drive/folder?
I would like to do this without changing the source of each layer as there would be almost a thousand of them.

Example:

Currently I have

M:\Planning\Development Plan\city sector.lyr
M:\Planning\Development Plan\city sector.shp
F:\Planning policy\Shapefiles\BDY.shp
M:\Directorates\Planning\08Plan\BDY.lyr

I want to change this to

M:\Directorates\Planning\Development Plan\city sector.lyr
M:\Directorates\Planning\Development Plan\city sector.shp
M:\Directorates\Planning\Planning Policy\Shapefiles\BDY.shp
M:\Directorates\Planning\08Plan\BDY.lyr

Basically I want ArcGIS Desktop to only search for data in M:\Directorates\Planning\ Every time ArcGIS tries to open an .mxd  that contains layer/shp/feature class files that had previously been located in M:\Planning\ or F:\ it will instead look in M:\Directorates\Planning\

Is there a way this can be done?

Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi thanks very much for the reply.
Could I use that script with the following folder references removed so that all folders in M:\Planning\ and F:\ are replaced or do I have to do it by individual folder?


import arcpy, os from arcpy import env workspace = env.workspace = r"C:\Temp\Python"  mxdList = arcpy.ListFiles("*.mxd")  for mxd in mxdList:     mxd2 = workspace + os.sep + mxd     mapdoc = arcpy.mapping.MapDocument(mxd2)     mapdoc.findAndReplaceWorkspacePaths(r"M:\Planning\", r"M:\Directorates\Planning\")     mapdoc.findAndReplaceWorkspacePaths(r"F:\", r"M:\Directorates\Planning\")     mapdoc.save()  del mapdoc


Also do I have to enter that script into all current .mxd files or can it be entered directly into ArcGIS just once?

Thanks again



Yes, this is possible, but takes a little bit more coding.  Here's an example:

import arcpy, os from arcpy import env from arcpy import mapping workspace = env.workspace = r"F:"  mxdList = arcpy.ListFiles("*.mxd")  for mxd in mxdList:     mxd2 = workspace + os.sep + mxd     mapdoc = arcpy.mapping.MapDocument(mxd2)     for df in mapping.ListDataFrames(mapdoc):         for layer in mapping.ListLayers(mxd, "", df):             if "F:\\" in layer.dataSource:                 origPath = layer.dataSource.split("\\")                 origPath2 = ""                 for x in origPath[0:-1]:                     origPath2 += x + os.sep                 origPath2 = origPath2[0:-1]                 dataSource = layer.dataSource.strip("F:\\")                 dataSource = dataSource.split("\\")                 path = ""                 for y in dataSource[0:-1]:                     path += y + os.sep                 path = path[0:-1]                 mapdoc.findAndReplaceWorkspacePaths(origPath2, r"M:\Directorates\Planning" + os.sep + path)             elif "M:\\Planning\\" in layer.dataSource:                 origPath = layer.dataSource.split("\\")                 origPath2 = ""                 for x in origPath[0:-1]:                     origPath2 += x + os.sep                 origPath2 = origPath2[0:-1]                 dataSource = layer.dataSource.strip("M:\\Planning\\")                 dataSource = dataSource.split("\\")                 path = ""                 for y in dataSource[0:-1]:                     path += y + os.sep                 path = path[0:-1]                 mapdoc.findAndReplaceWorkspacePaths(origPath2, r"M:\Directorates\Planning" + os.sep + path)     mapdoc.save()  del mapdoc


I have not tested this extensively, but had a similar version of this script work successfully.  Also, this will need to be only executed once.  The script will loop through all MXDs in the workspace directory:

workspace = env.workspace = r"F:"


In the above example, it will find all the MXDs on the F drive.  You can update this to the folder location where your MXDs are.

View solution in original post

0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
You could use the findAndReplaceWorkspacePaths to accomplish this.  Here is an example:

import arcpy, os
from arcpy import env
workspace = env.workspace = r"C:\Temp\Python"

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd2 = workspace + os.sep + mxd
    mapdoc = arcpy.mapping.MapDocument(mxd2)
    mapdoc.findAndReplaceWorkspacePaths(r"M:\Planning\Development Plan", r"M:\Directorates\Planning\Development Plan")
    mapdoc.findAndReplaceWorkspacePaths(r"F:\Planning policy\Shapefiles", r"M:\Directorates\Planning\Planning Policy\Shapefiles")
    mapdoc.findAndReplaceWorkspacePaths(r"M:\Directorates\Planning", r"M:\Directorates\Planning\08Plan")
    mapdoc.save()

del mapdoc
0 Kudos
IanCrabb
New Contributor
You could use the findAndReplaceWorkspacePaths to accomplish this.  Here is an example:

import arcpy, os
from arcpy import env
workspace = env.workspace = r"C:\Temp\Python"

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd2 = workspace + os.sep + mxd
    mapdoc = arcpy.mapping.MapDocument(mxd2)
    mapdoc.findAndReplaceWorkspacePaths(r"M:\Planning\Development Plan", r"M:\Directorates\Planning\Development Plan")
    mapdoc.findAndReplaceWorkspacePaths(r"F:\Planning policy\Shapefiles", r"M:\Directorates\Planning\Planning Policy\Shapefiles")
    mapdoc.findAndReplaceWorkspacePaths(r"M:\Directorates\Planning", r"M:\Directorates\Planning\08Plan")
    mapdoc.save()

del mapdoc


Hi thanks very much for the reply.
Could I use that script with the following folder references removed so that all folders in M:\Planning\ and F:\ are replaced or do I have to do it by individual folder?


import arcpy, os
from arcpy import env
workspace = env.workspace = r"C:\Temp\Python"

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd2 = workspace + os.sep + mxd
    mapdoc = arcpy.mapping.MapDocument(mxd2)
    mapdoc.findAndReplaceWorkspacePaths(r"M:\Planning\", r"M:\Directorates\Planning\")
    mapdoc.findAndReplaceWorkspacePaths(r"F:\", r"M:\Directorates\Planning\")
    mapdoc.save()

del mapdoc


Also do I have to enter that script into all current .mxd files or can it be entered directly into ArcGIS just once?

Thanks again
0 Kudos
MathewCoyle
Frequent Contributor
As a side note, I'd also look at using UNC paths vs mapped drives for network resources.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi thanks very much for the reply.
Could I use that script with the following folder references removed so that all folders in M:\Planning\ and F:\ are replaced or do I have to do it by individual folder?


import arcpy, os from arcpy import env workspace = env.workspace = r"C:\Temp\Python"  mxdList = arcpy.ListFiles("*.mxd")  for mxd in mxdList:     mxd2 = workspace + os.sep + mxd     mapdoc = arcpy.mapping.MapDocument(mxd2)     mapdoc.findAndReplaceWorkspacePaths(r"M:\Planning\", r"M:\Directorates\Planning\")     mapdoc.findAndReplaceWorkspacePaths(r"F:\", r"M:\Directorates\Planning\")     mapdoc.save()  del mapdoc


Also do I have to enter that script into all current .mxd files or can it be entered directly into ArcGIS just once?

Thanks again



Yes, this is possible, but takes a little bit more coding.  Here's an example:

import arcpy, os from arcpy import env from arcpy import mapping workspace = env.workspace = r"F:"  mxdList = arcpy.ListFiles("*.mxd")  for mxd in mxdList:     mxd2 = workspace + os.sep + mxd     mapdoc = arcpy.mapping.MapDocument(mxd2)     for df in mapping.ListDataFrames(mapdoc):         for layer in mapping.ListLayers(mxd, "", df):             if "F:\\" in layer.dataSource:                 origPath = layer.dataSource.split("\\")                 origPath2 = ""                 for x in origPath[0:-1]:                     origPath2 += x + os.sep                 origPath2 = origPath2[0:-1]                 dataSource = layer.dataSource.strip("F:\\")                 dataSource = dataSource.split("\\")                 path = ""                 for y in dataSource[0:-1]:                     path += y + os.sep                 path = path[0:-1]                 mapdoc.findAndReplaceWorkspacePaths(origPath2, r"M:\Directorates\Planning" + os.sep + path)             elif "M:\\Planning\\" in layer.dataSource:                 origPath = layer.dataSource.split("\\")                 origPath2 = ""                 for x in origPath[0:-1]:                     origPath2 += x + os.sep                 origPath2 = origPath2[0:-1]                 dataSource = layer.dataSource.strip("M:\\Planning\\")                 dataSource = dataSource.split("\\")                 path = ""                 for y in dataSource[0:-1]:                     path += y + os.sep                 path = path[0:-1]                 mapdoc.findAndReplaceWorkspacePaths(origPath2, r"M:\Directorates\Planning" + os.sep + path)     mapdoc.save()  del mapdoc


I have not tested this extensively, but had a similar version of this script work successfully.  Also, this will need to be only executed once.  The script will loop through all MXDs in the workspace directory:

workspace = env.workspace = r"F:"


In the above example, it will find all the MXDs on the F drive.  You can update this to the folder location where your MXDs are.
0 Kudos
IanCrabb
New Contributor
Yes, this is possible, but takes a little bit more coding.  Here's an example:

import arcpy, os
from arcpy import env
from arcpy import mapping
workspace = env.workspace = r"F:"

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd2 = workspace + os.sep + mxd
    mapdoc = arcpy.mapping.MapDocument(mxd2)
    for df in mapping.ListDataFrames(mapdoc):
        for layer in mapping.ListLayers(mxd, "", df):
            if "F:\\" in layer.dataSource:
                origPath = layer.dataSource.split("\\")
                origPath2 = ""
                for x in origPath[0:-1]:
                    origPath2 += x + os.sep
                origPath2 = origPath2[0:-1]
                dataSource = layer.dataSource.strip("F:\\")
                dataSource = dataSource.split("\\")
                path = ""
                for y in dataSource[0:-1]:
                    path += y + os.sep
                path = path[0:-1]
                mapdoc.findAndReplaceWorkspacePaths(origPath2, r"M:\Directorates\Planning" + os.sep + path)
            elif "M:\\Planning\\" in layer.dataSource:
                origPath = layer.dataSource.split("\\")
                origPath2 = ""
                for x in origPath[0:-1]:
                    origPath2 += x + os.sep
                origPath2 = origPath2[0:-1]
                dataSource = layer.dataSource.strip("M:\\Planning\\")
                dataSource = dataSource.split("\\")
                path = ""
                for y in dataSource[0:-1]:
                    path += y + os.sep
                path = path[0:-1]
                mapdoc.findAndReplaceWorkspacePaths(origPath2, r"M:\Directorates\Planning" + os.sep + path)
    mapdoc.save()

del mapdoc


I have not tested this extensively, but had a similar version of this script work successfully.  Also, this will need to be only executed once.  The script will loop through all MXDs in the workspace directory:

workspace = env.workspace = r"F:"


In the above example, it will find all the MXDs on the F drive.  You can update this to the folder location where your MXDs are.


Fantastic! Thanks again. This should make things much much easier. Am I right in thinking that I could run this script twice once for the F:\ and once for local C:\?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Yes, you can run this script twice.  You will just need to update the code to use the C:\ drive.
0 Kudos
IanCrabb
New Contributor
Yes, you can run this script twice.  You will just need to update the code to use the C:\ drive.


Brilliant! You�??ve been very helpful indeed. 😄
0 Kudos