Delete Rows in multiple Files

694
4
05-23-2012 02:38 AM
floLang
New Contributor II
Hello everybody !

i have several Shapefiles containing Points with Values 0 to 4.
I`d like to Delete the Point Features with Value '0'.
As a manual task, i tried it like this:
Select all '0' by sql request, execute the tool "Delete Rows", done.
Now i try to automate this Workflow with the Model Builder.
Is there a possibility to accomplish this task with the Model Builder at all?

I tried batching and file lists, but the main problem is SQL Statement of my Selection - how do i tell the sql engine to select the features from all input datasets ?

I am quite new to automation - thanks in advance for any answer.

with best regards

Flol
0 Kudos
4 Replies
TimHopper
Occasional Contributor III
Flo,

You'll want to use an iterator to cycle through all of the shapefiles within a particular folder.  It will then apply all downstream processes (Select by Attribute and Delete Rows) to each input shapefile.  Hopefully you're using v10.0, as doing this prior to that version will prove tricky using only model builder.

Specifically, you'll want to use the Iterate Files tool for your shapefiles.
0 Kudos
markdenil
Occasional Contributor III
The key is not to "select the features from all input datasets", but rather to select the features from each of the input datasets.
A simple script should handle looping through a list of shapefiles.
Build the list either with ListFeatureClasses or manually, and itterate it.

Feed the sql query into MakeFeatureLayer to select the proper (doomed) features.
Feed the resulting layer to DeleteFeatures (rather than DeleteRows)
Loop around again for the next shapefile.

Itteration is possible in Model Builder, but cleaner and more reliable in a python script.

One way of approaching it is that you can build a model that runs 'once through' just one shapefile, then export the model to a script and wrap the basic code in a loop.
It may look sorta like this: 
import arcpy
arcpy.env.workspace = r"c:\WhereTheShapesfileLive"
thelist_of_shapefiles = arcpy.ListFeatureClasses('*')
sqlBit = '"Value" = 0'
for each in thelist_of_shapefiles: 
    layer = each[:-4] + "Layer"
    arcpy.MakeFeatureLayer_management (each, layer, sqlBit)
    arcpy.DeleteFeatures_management(layer)
0 Kudos
floLang
New Contributor II
Hello rthopper and mdenil !

Thank you for your answers ! sadly i am still working with 9.3.1 -
the Python Script is a very good idea, ill try to code my process completely in Python now.
thank you both very much !
0 Kudos
markdenil
Occasional Contributor III
There is nothing in the script that would not work in 9.3.1 with a 9.3 geoprocessor object instead of arcpy.
import arcgisscripting
gp = arcgisscripting(9.3)
gp.workspace = r"c:\WhereTheShapefilesLive"
thelist_of_shapefiles = gp.ListFeatureClasses('*')
sqlBit = '"Value" = 0'
for each in thelist_of_shapefiles: 
    layer = each[:-4] + "Layer"  # slices off the '.shp' part of the name and adds suffix 'Layer'
    gp.MakeFeatureLayer_management (each, layer, sqlBit)
    gp.DeleteFeatures_management(layer)
0 Kudos