[GIS] How to Make a New Shapefile Based on Attributes of an Existing Shapefile

arcgis-desktoparcpyerror-999999python

In my last post I had an issue where I wanted to create multiple shapefiles based on only one attribute field (years). How to do it was answered in that post. I want to make that tool more dynamic so that the user can select any field and any value and make a shapefile for it. Any suggestions on how to use the tool dynamic so that fields and the operator(<,<=,=,>,>=) can also be defined by the user itself.


I tried the following:

import arcpy
fc = arcpy.GetParameterAsText(0)
yrs = arcpy.GetParameterAsText(1)
yrs = yrs.split(';')
subtract = ''
for yr in yrs:
     where = '"arcpy.GetParameterAsText(3)" arcpy.GetParameterAsText(2) ' + str(yr) + subtract
     subtract = ' AND "arcpy.GetParameterAsText(3)" > ' + str(yr)
     filename = str(arcpy.GetParameterAsText(3)) + '.shp'
     arcpy.FeatureClassToFeatureClass_conversion(fc, "arcpy.GetParameterAsText(4)", filename, where)

del yr

I get the following error message:
: ERROR 999999: Error executing function.
An invalid SQL statement was used.
An invalid SQL statement was used.
Failed to execute (FeatureClassToFeatureClass).

Also attaching the screenshot for properties while adding the script.

enter image description here

enter image description here

Best Answer

The built-in Feature Class to Feature Class tool does everything you want.

If you want more control than that, you can create a script tool and customize its validation behavior if necessary (for example, to list all the unique values of a chosen field).

To simply provide a drop-down of available fields you don't need to customize the behavior, just set up an input field parameter that is "Obtained from" the input feature class or feature layer parameter.

Obtained from parameter setup
(source: arcgis.com)

If you want to limit what the user can enter to a single SQL expression where the operator is chosen from a list, you could create a String parameter and set up its Filter property with the desired values. Then, in the script you would create the SQL where clause with Python, using string formatting (new or old styles) or concatenation to build the statement from the variable elements (field name, operator and value).