0

I am trying to Find out simple Geoprocessing Tool that can physically re-arrange the fields of attribute table (for both feature class + tables), I have found some posts here, but nether of them have provide a good and a simple tool that can do the job.

  • 3
    I've seen this issue addressed several times on SE. There is no built in way to change the schema of a table/feature class. However, ArcGIS Diagrammer is capable of doing what you are asking with a small learning curve. Additionally there are many other ways to do the same thing. Most notably, Python scripting. I will provide an answer with a script tool that can do this. However, there are limitations to the tool that I will explain. – GeoJohn May 1 '15 at 19:50
  • 2
    Just as I was about to flag as a duplicate... You can also see gis.stackexchange.com/questions/5873/… which has some further options. – Chris W May 1 '15 at 20:23
  • 1
    There are several posts that address your question. If you are unsatisfied with the answers, the preferred procedure is to start a bounty on one of the other duplicates. – Aaron May 1 '15 at 20:24
2

The following script can do what you are asking. There are some catches though...

  1. This will not change the original feature class, it makes a copy with reordered fields.
  2. There is a limit of twenty fields. This could be adjusted by adding more lines of code for each additional field beyond twenty.
  3. This is kind of a dirty way of doing things. It isn't very pythonic, efficient, or cool. But it works.
  4. It is designed to work with feature classes(not tables). However, a table could be run with it by removing the line that adds the "SHAPE@" token to the field list.

Script:

import arcpy, os, sys
from arcpy import env

arcpy.env.overwriteOutput = True

inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outName = arcpy.GetParameterAsText(2)
field1 = arcpy.GetParameterAsText(3)
field2 = arcpy.GetParameterAsText(4)
field3 = arcpy.GetParameterAsText(5)
field4 = arcpy.GetParameterAsText(6)
field5 = arcpy.GetParameterAsText(7)
field6 = arcpy.GetParameterAsText(8)
field7 = arcpy.GetParameterAsText(9)
field8 = arcpy.GetParameterAsText(10)
field9 = arcpy.GetParameterAsText(11)
field10 = arcpy.GetParameterAsText(12)
field11 = arcpy.GetParameterAsText(13)
field12 = arcpy.GetParameterAsText(14)
field13 = arcpy.GetParameterAsText(15)
field14 = arcpy.GetParameterAsText(16)
field15 = arcpy.GetParameterAsText(17)
field16 = arcpy.GetParameterAsText(18)
field17 = arcpy.GetParameterAsText(19)
field18 = arcpy.GetParameterAsText(20)
field19 = arcpy.GetParameterAsText(21)
field20 = arcpy.GetParameterAsText(22)

fieldList = ["SHAPE@"]

arcpy.AddMessage(" ")

arcpy.AddMessage("Appending field choices to new List ")

arcpy.AddMessage(" ")

if (field1 != ""):
    fieldList.append(field1)
if (field2 != ""):
    fieldList.append(field2)
if (field3 != ""):
    fieldList.append(field3)
if (field4 != ""):
    fieldList.append(field4)
if (field5 != ""):
    fieldList.append(field5)
if (field6 != ""):
    fieldList.append(field6)
if (field7 != ""):
    fieldList.append(field7)
if (field8 != ""):
    fieldList.append(field8)
if (field9 != ""):
    fieldList.append(field9)
if (field10 != ""):
    fieldList.append(field10)
if (field11 != ""):
    fieldList.append(field1)
if (field12 != ""):
    fieldList.append(field12)
if (field13 != ""):
    fieldList.append(field13)
if (field14 != ""):
    fieldList.append(field14)
if (field15 != ""):
    fieldList.append(field15)
if (field16 != ""):
    fieldList.append(field16)
if (field17 != ""):
    fieldList.append(field17)
if (field18 != ""):
    fieldList.append(field18)
if (field19 != ""):
    fieldList.append(field19)
if (field20 != ""):
    fieldList.append(field20)

arcpy.AddMessage(" ")

oldFieldList = arcpy.ListFields(inFC)

fieldTypes = []

numOfFields = len(fieldList)

fieldIndex = 1

reorderedFields = []

for fld in fieldList:
    for f in oldFieldList:
        if f.name == fld:
            arcpy.AddMessage(f.name)
            reorderedFields.append(f)
            break

arcpy.AddMessage(" ")

arcpy.AddMessage(reorderedFields)

desc = arcpy.Describe(inFC)
geoType = desc.shapeType.upper()
spatRef = arcpy.Describe(inFC).spatialReference

arcpy.CreateFeatureclass_management(outLoc, outName, geoType, "", "", "", spatRef)
newFeat = os.path.join(outLoc, outName)

for flds in reorderedFields:
    if (flds.type == "String"):
        fLength = flds.length
        arcpy.AddField_management(newFeat, flds.name, flds.type, "", "", str(fLength))
    else:
        arcpy.AddField_management(newFeat, flds.name, flds.type)

arcpy.AddMessage(" ")

arcpy.AddMessage(fieldList)

arcpy.AddMessage(" ")

arcpy.AddMessage("Features will be copied with new Schema...")

count = 0

with arcpy.da.SearchCursor(inFC, fieldList) as cursor:
    for row in cursor:
        insertCursor = arcpy.da.InsertCursor(newFeat, (fieldList))
        if (numOfFields == 21):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]) + ', ' + str(row[20]))
            count += 1
        elif (numOfFields == 20):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]))
            count += 1
        elif (numOfFields == 19):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]))
            count += 1
        elif (numOfFields == 18):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]))
            count += 1
        elif (numOfFields == 17):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]))
            count += 1
        elif (numOfFields == 16):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]))
            count += 1
        elif (numOfFields == 15):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]))
            count += 1
        elif (numOfFields == 14):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]))
            count += 1
        elif (numOfFields == 13):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]))
            count += 1
        elif (numOfFields == 12):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]))
            count += 1
        elif (numOfFields == 11):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]))
            count += 1
        elif (numOfFields == 10):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]))
            count += 1
        elif (numOfFields == 9):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]))
            count += 1
        elif (numOfFields == 8):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]))
            count += 1
        elif (numOfFields == 7):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]))
            count += 1
        elif (numOfFields == 6):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]))
            count += 1
        elif (numOfFields == 5):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]))
            count += 1
        elif (numOfFields == 4):
            insertCursor.insertRow((row[0], row[1], row[2], row[3]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]))
            count += 1
        elif (numOfFields == 3):
            insertCursor.insertRow((row[0], row[1], row[2]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]))
            count += 1
        elif (numOfFields == 2):
            insertCursor.insertRow((row[0], row[1]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]))
            count += 1

You will need to setup the script tool with all of the parameters for each field.

  • Thanks GeoJohn, may you please do me a favor by providing me with the procedure how to create the tool with its parameters sine i have no experience in this, thanks – Ahmad saleh May 2 '15 at 10:40
  • have a look at this screenshot1 and this screenshot2... have I done anything wrong since the fields doesn't displayed in the drop menu – Ahmad saleh May 2 '15 at 12:48
  • You need to set the "obtained from" property of all the field parameters to the input FC. Other than that, it looks correct. – GeoJohn May 4 '15 at 17:12
  • Hi GeoJohn, Thanks for your help, "obtained from" works fine with me, but when I try to run the tool and try to set the output work space it shows "The output dataset already exists ", have a look at this one two three, Again thanks for all help. – Ahmad saleh May 6 '15 at 16:23
  • Make sure that the "Direction" parameter is set to "Input" for all of the parameters. It is kind of counter intuitive in a way. Anything that you are giving to the tool as information on how to run is "Input" even if it is referencing output. Honestly I don't know if I've ever used Direction with "output". Usually output is handled within the script itself. – GeoJohn May 6 '15 at 19:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.