1

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
try:
 import arcgisscripting
 gp = arcgisscripting.create()
except:
 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):
 Fields.Reset()
 Field = Fields.Next()
 while Field:
 FieldName = str(Field.name)
 if x<len(NewOrder) and NewOrder[x].lower() == FieldName.lower(): 
 Layers[y] = Layers[y] + FieldName + " " + NewOrder[x] + " VISIBLE"
 x=x+1
 else:
 Layers[y] = Layers[y] + FieldName + " " + FieldName + " HIDDEN" 
 Field = Fields.Next()
 if Field:
 Layers[y] = Layers[y] + "; "
 FieldName = str(Field.name)
 else:
 print Layers[y]
 Layers.append("")
 y=y+1
 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")
 z=z+1
#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 
try:
 gp.CreateFeatureClass(FC_Out_WS,FC_Out_Name,fc_in_stype,LayerList,"#","#",fc_in_sr)
 gp.addmessage(gp.getmessages()+ "\n")
except:
 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
david_p
1,8031 gold badge20 silver badges34 bronze badges
asked Jun 12, 2013 at 21:48
4
  • I am really hoping that ArcGIS 10.2 will make re-ordering fields much easier Commented Jun 12, 2013 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? Commented Jun 13, 2013 at 9:52
  • Sure thing, where can I upload the script to you? Commented Jun 13, 2013 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. Commented Jun 19, 2013 at 21:36

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.