Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a few dozen feature classes, one of which contains the field I am looking for. They are stored in several different file geodatabases.

Is there a quick way of searching each feature class to find the one that has my field? Or do I have to check each one's attribute table in Catalog? Some of the files have the same names but are stored in separate locations.

I would like to be able to do this inside a Calculate Value tool in ModelBuilder, so that each time I needed to, I could run the model on a single file gdb and it would search through all the feature classes within that gdb and look for the field.

So far, I have a feature class iterator which iterates over all the feature classes in the file gdb recursively. Inside the Calculate Value I have the following code:

findField(r"%Feature%", "%Search Field%")

def findField(fc, fi):
  lst = arcpy.ListFields(fc)
  for f in lst:
    if f.name == fi:
      return fc
    else:
      return "not found"

The returned values are collected in Output Values as strings. The output for all the feature classes has been "not found", even though the field exists in at least 4 of them. enter image description here

share|improve this question
    
What are you trying to calculate the value of? Are you trying insert records into a table? It doesn't make much sense what you are trying to use the Calculate Value tool for... –  RyanDalton Oct 3 '12 at 5:21
    
I've put the Python code inside the Calculate Value to search for the field, and to return the path of the feature class as a string if it finds the field - see my answer below. –  Cindy Williams Oct 3 '12 at 6:32

4 Answers 4

up vote 1 down vote accepted

Found the answer with some help from the code in the answer to this question. I took all the field names, converted them to a string and then searched the string for my field. The code in the Calculate Value now looks as follows:

def findField(fc, fi):
  fieldList = arcpy.ListFields(fc)
  nameList = []
  for f in fieldList:
    nameList.append(f.name)
  str_lst = "!" + "!, !".join(nameList) + "!"
  return "found in" +fc if str_lst.find(fi) > 0 else "not found in " +fc

The Output Values now contains the full list of feature classes which has the field I searched for, and not found for the rest.

Collect Values output

share|improve this answer

Arabella, are your featureclasses in Feature Datasets? If so, @Aragon's solution will not work because you need to walk through the feature datasets and then check the featureclasses.

Using @Aragon's code as well as @gotchula's code from List all feature classes in GDB, including within feature datasets, I produced the following code that will read through all feature datasets and featureclasses in a geodatabase.

NOTE: By default ListFields appears to be case-sensitive. I have added code to convert both the user-defined fieldname and the ListField name to lowercase for case-insensitive searches. You probably would also want to disable most of the print statements, but I left them in there so you can follow the code as it is running. I also created the sub-function FindField within the so that I could call it multiple times in the script without having to re-code it.

import arcpy

def FindField(fc,myField):
    fieldList = arcpy.ListFields(fc)
    for field in fieldList:
        if str.lower(str(field.name)) == str.lower(myField):
            print "    " + fc + " contains fieldname: " + myField

myField = "test"
arcpy.env.workspace = "D:/test/data.gdb"

#Search root level featureclasses
for fc in arcpy.ListFeatureClasses():
    print "Searching root level Featureclasses..."
    print "  Searching " + fc
    FindField(fc,myField)

#Search Feature Datasets
for fds in arcpy.ListDatasets('','feature'):
    print "Searching FeatureDataset: " + fds

    for fc in arcpy.ListFeatureClasses('','',fds):
        print "  Searching Featureclass... " + fc
        FindField(fc,myField)
share|improve this answer
    
I would be running this in a model, so I adapted it slightly and tested it. I'm not sure what I want the Calculate Value to return though - could I return "true" if FindField(fc,myField) =="true" else "false"? –  Cindy Williams Oct 2 '12 at 5:30
    
Ah, just realised that my previous comment would not tell me anything. Also running that boolean test inside the model returns nothing. –  Cindy Williams Oct 2 '12 at 5:32
    
Calcluate Value? Boolean test? I think you should clarify your question about what exactly you are after. –  RyanDalton Oct 2 '12 at 14:26
    
I've edited the question. –  Cindy Williams Oct 3 '12 at 5:10

Check out this function from by Bjorn Kuiper to test if a field exists :

def FieldExist(featureclass, fieldname):
    fieldList = arcpy.ListFields(featureclass, fieldname)

    fieldCount = len(fieldList)

    if (fieldCount == 1):
        return True
    else:
        return False

with the following example of use:

    if (not FieldExist(myFeatureClass, "myField")):
      arcpy.AddError("Field 'myField' does not exist in " + myFeatureClass)
      sys.exit()
share|improve this answer
    
I did end up using something similar a while back, but it didn't quite give the output I was looking for. –  Cindy Williams Oct 2 '12 at 5:17

you can use arcpy:

import arcpy

myField = "test"

env.workspace = "D:/test/data.gdb"
fcs = arcpy.ListFeatureClasses()

for f in fcs:
    fieldList = arcpy.ListFields(f)
    for field in fieldList:   
         if field.name == myField:
             print f

Beside this you can use os.walk for files in your drive as:

path = r"C:"
for root, dirs, files in os.walk(path):
    for fileName in files:
            .........

i hope it helps you....

share|improve this answer
    
I tried it, and while the code makes sense, it doesn't do anything. I tested it by using a field which is in four of my feature classes - the script ran but did not output anything. –  Cindy Williams Jun 6 '12 at 8:48
    
oh sorry, i forget to loop fields in fieldset, now i update my answer and i have tested it. –  Aragon Jun 6 '12 at 9:08
    
I have tried with the updated code, it runs but no output even though the field is in several feature classes within the geodatabase. –  Cindy Williams Jun 8 '12 at 5:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.