I have a dataset with about 300 different shapefiles. I want to Merge all the shapefiles into one, but I need to keep the shapefile name. I have added a field to each of these shapefiles, but can't figure out how to populate the table with the name of the shapefile. I tried to download the Arc Toolbox "AddFieldName", but it didn't work. I also tried some of the Python codes and they also didn't work.
This could be done in Python. Try running this before you run the merge.
import arcpy import os SHPfolder = r'...path to folder where .shps are kept...' for x in os.listdir(SHPfolder): with arcpy.da.UpdateCursor(SHPfolder + '/' + x, ['shpName']) as cursor: #Change field name if necessary for row in cursor: row = str(x) cursor.updateRow(row) del cursor
These links may assist you ... I believe %name% is reserved for model use....
This model may help you as well (I have no personal experience with it)....
To extend Mitch's answer, if your data is in a geodatabase rather than a folder, try the code below.
import arcpy file_name_field = 'FNAME' input_gdb_or_folder = r'c:\my.gdb' arcpy.env.workspace = input_gdb_or_folder feature_classes = arcpy.ListFeatureClasses() for fc in feature_classes: print(fc) # just so you know what the script is processing # add field to hold the file name if it does not exist existing_fields = [f.name for f in arcpy.ListFields(fc)] if file_name_field not in existing_fields: arcpy.management.AddField(fc, file_name_field, 'TEXT', field_length=200) # write the file name into each row of the file name filed with arcpy.da.UpdateCursor(fc, [file_name_field]) as uc: for row in uc: uc.updateRow([str(fc)]) del row, uc
For anyone coming across this question (4 years later), this functionality is now available in the ArcGIS Pro Merge tool: https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/merge.htm
The tool has an "Add source information to output" checkbox - if you check it, it'll add a field named MERGE_SRC to the output:
The values in the MERGE_SRC field will indicate the input dataset path or layer name that is the source of each record in the output.
When I used it, I added layers to the tool from my Map, and the values in the resulting MERGE_SRC field looked like "GroupName\LayerName" (as named in the Map's Contents list). I haven't tried it with data added to the tool directly from browsing - maybe it puts the full pathname if you do it that way.