Select to view content in your preferred language

Model Builder - Deleting fields in 80+ Files in a folder

5303
2
12-02-2015 09:56 PM
YaneevGolombek
Emerging Contributor

Hi.

Thanks in advance for any advice.

I have a folder with 80+ shapefiles. I am trying to automate a process in model builder that will do the following:

  1. Delete all fields in each shapefile except a unit ID
  2. Export the resulting shapefile with deleting fields into a geodatabase.

I attempted to do this with a a feature class iterator, delete field function and copy features to a database I created. However I cannot get the model to work.

Any thoughts from experience Model Builder folks will be extremely helpful.

Thanks.

0 Kudos
2 Replies
FreddieGibson
Honored Contributor

ModelBuilder wouldn't allow you to delete the fields with the tools that are available out-of-the-box. You could use the Feature Classes Iterator with the recursive property set to true to locate all of the feature classes, but you would need to write a python tool or function to get the names of the fields that you'd be able to delete from the feature class (i.e. fields that aren't of type Geometry or OID). The logic needed could probably be execute with a single line in the Calculate Value tool or at least three lines in a script tool. The logic would be based on the following to list the needed fields to create the MultiValue object needed to pass to the Delete Field (Data Management) tool.

[f.name for f in arcpy.Describe(fc).fields if f.type not in ("Geometry", "OID") and f.name not in ("UnitID")]

Iterate Feature Classes

http://desktop.arcgis.com/en/desktop/latest/tools/modelbuilder-toolbox/iterate-feature-classes.htm

Examples of using iterators in ModelBuilder

http://desktop.arcgis.com/en/desktop/latest/tools/modelbuilder-toolbox/examples-of-using-iterators-i...

Calculate Value

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/calculate-value.htm

Delete Field

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/delete-field.htm

If your end goal is to delete the fields so that you can copy the feature class to another source you may be able to accomplish this task with the out-of-the-box tools in a number of ways. The first way that I could think of would be to create a new feature class with the CreateFeatureClass (Data Management) tool, add your needed field with the Add Field (Data Management) tool, and then use the Append (Data Management) tool to only map the Shape and ID fields. A quicker way to accomplish this would be to simply call the Feature Class to Feature Class (Conversion) tool and provide a fieldmappings object that only retains the few fields that you're needing to retain.

Create Feature Class

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/create-feature-class.htm

Add Field

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/add-field.htm

Append

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/append.htm

Feature Class To Feature Class

http://desktop.arcgis.com/en/desktop/latest/tools/conversion-toolbox/feature-class-to-feature-class....

Using the field mapping control

http://desktop.arcgis.com/en/desktop/latest/analyze/executing-tools/using-the-field-mapping-control....

RebeccaStrauch__GISP
MVP Emeritus

Not ModelBuilder, but how about a small python script?

I suggest you run the FeatureClassToFeatureClass_conversion once mannualy to see the correct mapping, for example, I ran

The grabbed the python snippet from the results tab

arcpy.FeatureClassToFeatureClass_conversion(in_features="C:/__temp/Pts1_3338.shp", out_path="C:/__temp/test.gdb", out_name="pts1_3338", where_clause="", field_mapping="""PtID "PtID" true true false 5 Short 0 5 ,First,#,C:/__temp/Pts1_3338.shp,PtID,-1,-1""", config_keyword="")

so I could see the field mapping to use in the script.    Update the workspace, out FGDB, and the fieldmapping as needed.

import arcpy
import os
# update with the location of you files
arcpy.env.workspace = theWS =  r'C:\__temp'
# update with the target FGDB....assumes in same folder as the shapes, change if needed
outFGDB = arcpy.os.path.join(theWS, "test.gdb")

# grabs a list of the shapes
featureClasses = arcpy.ListFeatureClasses()
for fc in featureClasses:
    #outfile to be the same name as input, minus the .shp
    outfc = fc.strip(".shp")
    # field mapping from manual fun....{0} will be the "fc", i.e. the shapfile name
    fieldMapping = ("""PtID "PtID" true true false 5 Short 0 5 ,First,#,{0},PtID,-1,-1""".format(fc))
    #print ("{0}, {1}, {2}".format(fc, outfc, keepField))
    arcpy.FeatureClassToFeatureClass_conversion(fc, outFGDB, outfc, "", fieldMapping, "")
    

hope this helps.  BTW - it will take ALL the shapes from the workspace, so issolate if needed.  If one of the files doesn't have the field, the output will add the field, but values will be empty.  As always....test on a copy of the files until you get it the way you want.

0 Kudos