Skip To Content

Example: Register folders and databases listed in a text file

This example script reads a text file list of your data locations and registers each folder and database with ArcGIS Server. Although you can register databases and folders using Manager or ArcGIS for Desktop, it may be faster to use this script if you have many locations to register.

The input file should be structured like this:


dsName=pebbles|dsType=DATABASE|serverPath=C:\data\SDE_Connections\Connection to pebbles864.sde|clientPath=C:\dataSDE_Connections\Connection to pebbles864.sde
dsName=bedrock|dsType=DATABASE|serverPath=C:\data\SDE_Connections\Connection to bedrock.sde|clientPath=C:\dataSDE_Connections\Connection to armenia.sde
dsName=oaktree|dsType=DATABASE|serverPath=C:\data\SDE_Connections\Connection to oaktree.sde|clientPath=managed

  • dsName—A name for the data store item. Can be any name that you supply.
  • dsType—The type of data store item being registered. Can be FOLDER or DATABASE.
  • serverPath—The path to the data as seen by the server machine.
  • clientPath—The path to the data as seen by the publishers' machines. If not supplied, the server path will be used. If you supply the keyword managed, it means you are registering this data store item as ArcGIS Server's Managed Database.
  • hostname—The name of the publisher machine. Needed only when the clientPath is a folder-based path. If this property is omitted, the name of the machine running the script will be used.

You may also want to read About registering your data with the server before working with this script.

import sys,os

import getpass

import arcpy

import codecs

def main(argv=None):
    # Ask for admin user name and password
    username = raw_input("Enter user name: ")
    password = getpass.getpass("Enter password: ")
    # Ask for server name & port
    serverName = raw_input("Enter server name: ")
    serverPort = raw_input("Enter server port: ")
    # Create a connection file to the server and save it in the same location as that of the script
    except Exception, e:
            print e.message    
    agsConnection = os.path.join(os.curdir, serverName+".ags")
    if not os.path.isfile(agsConnection):
        print("Unable to connect to ArcGIS Server. Exiting.")

    # Input File that contains the data store information
    dataStoresFile = raw_input("Path to pipe-delimited text file containing datastore information: ")
    num = 0 
    datastores = {}
    for datastoreRow in readlinesFromInputFile(dataStoresFile):
        datastoreEntry = {}
        for index in range(len(datastoreRow)):
            datastoreProp = datastoreRow[index].split("=")
            if datastoreProp[0] == "dsName":
                datastoreEntry["connection_name"] = datastoreProp[1]
            if datastoreProp[0] == "dsType":
                datastoreEntry["datastore_type"] = datastoreProp[1]
            if datastoreProp[0] == "serverPath":
                datastoreEntry["server_path"] = datastoreProp[1]
            if datastoreProp[0] == "clientPath":
                datastoreEntry["client_path"] = datastoreProp[1]
            if datastoreProp[0] == "hostname":
                datastoreEntry["hostname"] = datastoreProp[1]
            # Add the datastore information to a dictionary
            datastores["datastore" + str(num)] = datastoreEntry
        num +=1

    # Call helper functions to register datastores

# A function that reads lines from the input file
def readlinesFromInputFile(filename, delim='|'):
    file =,'r','utf-8-sig')
    for line in file.readlines():
        # Remove the trailing whitespaces and the newline characters
        line = line.rstrip()
        if line.startswith('#') or len(line) == 0:
            pass # Skip the lines that contain # at the beginning or any empty lines
            # Split the current line into list
            yield line.split(delim)
def addDataStores(datastoresDict,agsConnection):
    for datastoreToAdd in datastoresDict:
        # Build the dictionary with the role name and description               
        datastoresDict[datastoreToAdd]["connection_file"] =  agsConnection
        print "Adding the datastore: " + datastoresDict[datastoreToAdd]['connection_name']
            print "Successfully added the datastore: " + datastoresDict[datastoreToAdd]['connection_name'] 
        except Exception, e:
            print "Adding of the datastore: " + datastoresDict[datastoreToAdd]['connection_name'] + " failed."
            print e.message

if __name__ == "__main__":