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.

Is there any phyton/VB script which will add a field to shapefile which will be corresponding to first three characters from the shapefile name ?

I am working in modelbuilder and arcgis 10.

Thanks in advance.

share|improve this question
add comment

2 Answers

you can do this with Add Field (Data Management).

Syntax

AddIndex_management (in_table, fields, {index_name}, {unique}, {ascending})

import arcpy
from arcpy import env

env.workspace = "C:/test.gdb"

inFeat = "cityoflondon.shp"
fieldLength = 10

#it will return "cit" from inFeat
fieldName = str(inFeat[:3])

arcpy.AddField_management(inFeat, fieldName, "TEXT", "", "", fieldLength)

i hope it helps you...

share|improve this answer
    
I forgot to add that i need to add field in multiple shapefiles at once. I have one hundred shapefiles. Sorry, my mistake! –  com May 11 '12 at 8:08
add comment

This code should do the trick. New field should be titled "New"

import arcpy
arcpy.env.workspace = r"c:\Temp"
shapename = "newshape.shp"
newfield = shapename[0:3]
arcpy.AddField_management(shapename,newfield,"TEXT","","","20")
share|improve this answer
    
shapename[0:3] would give the first 4 letters, no? –  Roy May 10 '12 at 16:51
1  
@Roy, This notation starts at and includes the first number, and ends at, but excludes, the last number. –  Get Spatial May 10 '12 at 16:54
1  
Thanks, indexing always gets me. Thank god for trial and error. –  Roy May 10 '12 at 17:54
add comment

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.