Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use this Reorder fields modelbuilder tool but for some reason the inline variable (%name%) for the output gets overwritten with each file pass until the last one. Otherwise it works fine, however, I need to process hundreds of shapefiles and rearrange the field order.

Any help or corrections would be greatly appreciated.

I've attached an image from the modelbuilder I am currently using. Here's the script:

Galindo Reorder Fields

import os,string,sys

    import arcgisscripting
    gp = arcgisscripting.create()
    import win32com.client
    gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

FC_In = sys.argv[1]
NewOrder = sys.argv[2]
FC_Out_WS = sys.argv[3]
FC_Out_Name = sys.argv[4]

strFields = gp.listfields(FC_In) 
strNewOrder = NewOrder 
Layers = [""]

# Get Field Collection into List
Fields = gp.listfields(FC_In) 

NewOrder = string.split(strNewOrder,";")
print Fields
print NewOrder

x=0 #NewOrder enumerator 
y=0 #Layers enumerator (first)

# Cycles through the input feature class creating field collections
while x<len(NewOrder):
    Field = Fields.Next()
    while Field:
        FieldName = str(
        if x<len(NewOrder) and NewOrder[x].lower() == FieldName.lower(): 
            Layers[y] = Layers[y] + FieldName + " " + NewOrder[x] + " VISIBLE"
            Layers[y] = Layers[y] + FieldName + " " + FieldName + " HIDDEN" 

        Field = Fields.Next()

        if Field:
            Layers[y] = Layers[y] + "; "
            FieldName = str(
            print Layers[y]

# Use Field collections to create Layers with proper order until all fields have been used
z=0 #Layers enumerator (second)
LayerList = ""
for Layer in Layers:
    if Layer <> "":
        if LayerList <> "":
            LayerList=LayerList + ";"
        LayerList=LayerList + "Layer" + str(z)
        print LayerList
        gp.MakeFeatureLayer(FC_In, "Layer" + str(z),"#","#",Layer)
        gp.addmessage(gp.getmessages()+ "\n")

#Collect Info about Input Feature Class
dsc = gp.describe(FC_In)
fc_in_stype = dsc.shapetype
fc_in_sr = dsc.spatialreference

# Create new empty Feature class with desired fields 
    gp.addmessage(gp.getmessages()+ "\n")
    gp.addmessage(gp.getmessages()+ "\n")

# Append Records to new feature class
print FC_Out_WS + "\\" + FC_Out_Name
print FC_In
gp.append_management(FC_In, FC_Out_WS + "\\" + FC_Out_Name, "NO_TEST")
gp.addmessage(gp.getmessages()+ "\n")

del gp
share|improve this question
I am really hoping that ArcGIS 10.2 will make re-ordering fields much easier – PolyGeo Jun 12 '13 at 22:15
Galindo, can you upload the scripting code or make the whole tool and code available from some other location? I expect the problem is in the python code? – Hornbydd Jun 13 '13 at 9:52
Sure thing, where can I upload the script to you? – galindo Jun 13 '13 at 14:53
Can anyone figure out how to make this script run with an inline variable "%name%"? Right now, it just works with one feature class at a time and I would like to process multiple FC's and store the output in a file directory. – galindo Jun 19 '13 at 21:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.