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
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 Fields.Reset()
Field = Fields.Next()
while Field:
FieldName = str(Field.name)
if x 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
No comments:
Post a Comment