Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anybody know how to add the shape file 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?

share|improve this question
up vote 5 down vote accepted

There is a script on the arcscripts website that should assist you in doing this

Summary A simple script for creating a field called "FILENAME" and attaching the filename of the shapefile to each entry in the attribute table. Use with as many shapefiles within a specified directory. Useful when picking apart shapefile entries and combining into a separate file to maintain a breadcrumb trail of the original shapefile name.

Run file within a directory containing all the shapfiles needing modification

I hope that this's what you're after

share|improve this answer
For info : doesn't work for Arcgis 10.X (Python code syntax has changed after Arcgis 9.x). – RobinP Sep 1 '15 at 9:57

this python code adds FILENAME field to all Featureclasses in a Dataset 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

    # Calculate field here
    arcpy.CalculateField_management(fc, field, "expression", "PYTHON")
share|improve this answer
works in version 10.2. but does not work in version 10.2.2. – Nate Bradshaw Jan 22 '15 at 18:23

Here is a link to an even better and easier tool using model builder.

share|improve this answer

If all the above does not work for you, due to version incompatibility or lack of knowledge (like it is for me), then try this workaround:

  • use the Export To CAD conversion tool and export all your feature classes from your geodatabase into a DWG file
  • the name of the feature class will be used to populate the Layer field for the CAD file
  • use the CAD to Geodatabase conversion tool to export your features back into a GDB
  • you will now have the layer name fields in your table for each single feature
  • use the Merge tool to combine your feature classes from the GDB into one single shapefile, which will have the layer attribute field with the original feature class name for you to identify the items
share|improve this answer

Your Answer


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.