Change Data Source to new GDB

758
9
12-15-2010 06:30 AM
StuartBlumberg
New Contributor II
Hello,

All I want to do is change the data source of all layers of all MXDs in a single folder from one FGDB to another.  For example I want the data sources to be changed from "Project.gdb" to "Project2010.gdb"

I've tried all the python code from the help topics and can't seem to get the right script.

Thanks.
Tags (2)
0 Kudos
9 Replies
DonovanCameron
Occasional Contributor II
There is a related forum on changing the data source using python.

There is a script offered there from the Resource Center, also note, that these scripts are offered as examples already installed as part of v10 in the toolbox.

Did that referred forum offer you your solution?
0 Kudos
StuartBlumberg
New Contributor II
The forum link you provided is for changing the data source from one folder path to another.  I need to change from one FGDB to another.
0 Kudos
DonovanCameron
Occasional Contributor II
I need to change from one FGDB to another.


I am pretty sure you can, the script just needs to be modified to do so.

Look at the help on the syntax for Updating and fixing data sources with arcpy.mapping

Go down to the Usage section and find the header(s):
Data is migrated from one workspace type to another workspace type

Individual datasets are moved in and out of feature datasets


Also look at the Known Limitations in regards to these specific processes. They will offer pointers on writing your script with that you can and cannot do currently.
0 Kudos
StuartBlumberg
New Contributor II
I've gone through those examples multiple times, trying different variations to get it to change the FGDB with no luck.  My problem is I don't know Python enough to correctly format the code.  Is there code out there specifically for my purpose?
0 Kudos
RDHarles
Occasional Contributor
You can do it with python, but if you just have one mxd to fix, simply right-click on the mxd in ArcCatalog, click "Set Data Source(s)...", then click the "Select All" then the "Replace All" buttons.  Finally, type the name of the new FGDB and save the mxd.
0 Kudos
StuartBlumberg
New Contributor II
rdharles,

If I have multiple MXDs in a folder and I need to change them all at once how would I do this?
0 Kudos
RDHarles
Occasional Contributor
Try this, worked for me...:

import arcpy, os

# Set current workspace
arcpy.env.workspace = os.getcwd()

# For every mxd in the current directory
for mxd in os.listdir(''):
    if mxd.endswith('.mxd'):
        print "Changing paths in "+mxd+"..."
        mxd = arcpy.mapping.MapDocument(mxd)
        # Note: Back slashes seemed to be required for it to work.
        mxd.findAndReplaceWorkspacePaths(r"c:\junk\fgdb\python\b.gdb", r"c:\junk\fgdb\python\a.gdb")        
        mxd.save()
del mxd
0 Kudos
StuartBlumberg
New Contributor II
Harles,

How does it know which folder the MXDs are in?  What if the MXDs are in a different folder than the GDB?  Thanks.
0 Kudos
RDHarles
Occasional Contributor

How does it know which folder the MXDs are in?
How are you running your scripts? (ArcToolBox, Pythonwin, command line, etc).
More than likely you just need to set the arcpy.env.workspace to where your .py scripts are.
e.g. arcpy.env.workspace = "c:/temp"
[I run my scripts from a windows command line so the workspace is always my current directory.]


What if the MXDs are in a different folder than the GDB?


Shouldn't matter as long as you use the correct replace path:
MapDocument.findAndReplaceWorkspacePaths(find_workspace_path, replace_workspace_path)
0 Kudos