Is there a way using Export XML Workspace Document to export every database connection from within a folder?

3393
11
11-17-2014 07:01 AM
MichaelRivera
Occasional Contributor II

Say I have 40 database connections in a folder, is there some way to just export every database that has a connection?

Instead of writing

in_data = "c:/data/StJohns.gdb"
out_file = "c:/data/StJohns.xml"
export_option = "SCHEMA_ONLY"
storage_type = "BINARY"
export_metadata = "METADATA"

# Execute ExportXMLWorkspaceDocument
arcpy.ExportXMLWorkspaceDocument_management(in_data, out_file, export_option, storage_type, export_metadata)

40 times?

0 Kudos
11 Replies
DuncanHornby
MVP Notable Contributor

A simple model should suffice as shown below:

Capture.PNG

Use the Iterate Workspace to return File GeoDatabases and tick on recursive if you want drill down into the sub-folders. You would then use inline substitution to ensure a unique file name for the Output file.

If you want to do this in just python then look at this function here.

0 Kudos
MichaelRivera
Occasional Contributor II

I'd like to do it with Python.  The Python function you listed is shown working for arcpy.Compact_management which would get the its "in_workspace" from

workspaces = arcpy.ListWorkspaces("*", "FileGDB")

but arcpy.ExportXMLWorkspaceDocument_management requires a "out_file" variable, so how would that work with workspaces = arcpy.ListWorkspaces("*", "FileGDB")?

0 Kudos
by Anonymous User
Not applicable

Hi Michael,

Try this:

import arcpy, os

arcpy.env.workspace = r"<path>"

workspaces = arcpy.ListWorkspaces("*", "FileGDB")

for workspace in workspaces:

    print workspace

    n,t = os.path.basename(workspace).split(".")

    xml = arcpy.env.workspace + os.sep + "{0}.xml"

    arcpy.ExportXMLWorkspaceDocument_management(workspace, xml.format(n))

0 Kudos
MichaelRivera
Occasional Contributor II

Is my path formatting incorrect?

I've tried

arcpy.env.workspace = r"D:/Valor/"

and the script bales pretty quick, don't even see a error message.

0 Kudos
Zeke
by
Regular Contributor III

If you use r before the string, use the regular Windows backslash in your path - \. Using a forward slash is for when you don't use r. Using it the way you have above, python interprets the forward slash as just that, a forward slash, which does not work on Windows.

0 Kudos
MichaelRivera
Occasional Contributor II

Windows 7 machine.

I've tried

  1. arcpy.env.workspace = r"D:\Valor\"
  2. and arcpy.env.workspace = r"D:/Valor/"
  3. and arcpy.env.workspace = "D:/Valor/"

None of them work.  The black box opens up quickly with no error message before closing.  Nothing happens

0 Kudos
Zeke
by
Regular Contributor III

Instead of setting the workspace, try just putting the path there. In another thread. Richard Fairhurst stated that setting the env environment settings in a stand-alone script didn't actually do anything.

0 Kudos
by Anonymous User
Not applicable

Where are you attempting to run the script? In an IDE like IDLE or PyScripter? Or directly in the python window in ArcMap?

If you want to run the script in the Command Prompt you will have to change directories to the location of the script and call it:

2014-11-25_11-29-06.jpg

I would recommend running the script in an IDE...I like PyScripter but some people like IDLE or PythonWin.

0 Kudos
MichaelRivera
Occasional Contributor II

Running the script by just double-clicking on it from its Windows folder.

0 Kudos