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 am new to model builder and have not been able to figure how to use part of the FC name: For example the first FC in my GDB containing around 100 files is "A1005_GPS_Residence", and I need to calculate the new field SID = "A1005". I have seen some posts with python and manually using the Field Calculator. I tried using one expression, shown in the screenshot, but Im not sure if I can combine field calculations with inline substitution or the like !%Name%![1:4]. Thanks for any info/help.

Screnshot of MB

share|improve this question
    
Just a suggestion but unless you know the desired substring is ALWAYS going to be the first 5 characters it might be better to use split or a regex to get at it. –  blah238 Sep 2 '12 at 21:19
    
I thought Expression: Reclass(!SID!), Expression Type: Python_9.3 and Code Block: def Reclass(sid): return %Name%[:5] might work but it interprets the value of %Name% as being a global variable name - have not found way to avoid that –  PolyGeo Sep 3 '12 at 1:33

1 Answer 1

I have attached a python script that should steer you in the right direction. You will find that custom naming in loops is much easier in python than model builder.

EDIT: Added additional code to address new information

# Import standard library modules
import arcpy, os, sys
from arcpy import env

# Allow for file overwrite
arcpy.env.overwriteOutput = True

# Set the workspace directory 
env.workspace = r"C:\temp.gdb" 

# Get the list of the featureclasses to process
fc_tables = arcpy.ListFeatureClasses()

# Loop through each file and perform the processing
for fc in fc_tables:
    print str("processing " + fc)

    # Define field name and expression
    field = "SID"
    expression = str(fc[:5]) #subsets first 5 characters of fc name

    # Create a new field with a new name
    arcpy.AddField_management(fc,field,"TEXT")

    # Calculate field here
    arcpy.CalculateField_management(fc, field, "expression", "PYTHON") 

enter image description here

share|improve this answer
    
Thanks so much for all the help and for the script. It saved me hours of trying to piece bits of python code together, and a nice way to start looking into arcpy. –  user9914 Sep 4 '12 at 18:32

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.