Does anybody know how to add the shapefile name to a field in the file's attribute table?
I've found a description:
'You have to use inline variable in CalculateField like %Name% in expresssion so that whatever the name of the file is, the string will be inserted based on Name of the dbase file !'
I've tried to CalculateField tool with %Name%, but I got message:
Executing: CalculateField "2011-11-6 15_41_point" DBFName %Name% PYTHON #
Start Time: Thu Dec 08 23:34:23 2011
WARNING 000405: No records within table
Succeeded at Thu Dec 08 23:34:24 2011 (Elapsed Time: 1.00 seconds)
Where is my mistake?
Answer
This python code adds FILENAME field to all Featureclasses (excluding those in Datasets) and populates with featureclass name.
# 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"P:\geodatabase.gdb\filename"
# 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 = "FILENAME"
expression = str(fc) #populates field
# Create a new field with a new name
arcpy.AddField_management(fc,field,"TEXT")
# Calculate field here
arcpy.CalculateField_management(fc, field, '"'+expression+'"', "PYTHON")
No comments:
Post a Comment