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?

5 Answers 5


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

    # Calculate field here
    arcpy.CalculateField_management(fc, field, '"'+expression+'"', "PYTHON")
  • works in version 10.2. but does not work in version 10.2.2. Commented Jan 22, 2015 at 18:23
  • Nothing happens if I type the code in the python console of arc map (10.6) It seems not to work with this version (?)
    – David
    Commented Jul 2, 2018 at 13:29

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

  • For info : doesn't work for Arcgis 10.X (Python code syntax has changed after Arcgis 9.x).
    – gisnside
    Commented Sep 1, 2015 at 9:57

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


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

I found this link to a US Fish and Wildlife Service ArcGIS Tool that does exactly what you're looking for. It worked like a charm!

  • This is the same tool described in an earlier answer
    – PolyGeo
    Commented Jul 13, 2017 at 22:18
  • The tool from US Fish and Wildlife Service only works if field name is "NAME". It can be made flexible by updating Calculate Field Tool 'Field Name' with %Field Name% Commented Aug 28, 2017 at 9:42

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