I have over 60 shapefiles, all in their own folders. I'm looking to add a field titled "Name" and then populate it with the name of the shapefile. I want to be able to do this so that I can then merge the files together while retaining a unique identifier for each. I can do this by hand of course, but would like to script it. I don't know Python and I'm familiar though not very experienced with ModelBuilder. I'm using ArcGIS 10.
[GIS] Batch shapefile merge, with adding and calculating field, in ArcPy
arcgis-10.0arcpy
Related Solutions
Works fine for me at 10.1. I made a slight modification (other than the obvious configuration variable changes) for the case when the Layer.datasetName
property is not supported:
#Select map
map_path = r"C:\GISData\BloomfieldTownship\BloomfieldTownship.mxd"
#define dictionary
PathDict = {
"SiteAddress": r"C:\GISData\BloomfieldTownship",
"RoadCenterline": r"C:\GISData\BloomfieldTownship",
"SchoolTaxDistrict": r"C:\GISData\BloomfieldTownship"}
import arcpy
mxd = arcpy.mapping.MapDocument(map_path)
for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
layername = lyr.datasetName if lyr.supports("datasetName") else ""
if layername in PathDict.keys():
new_path = PathDict[layername]
lyr.replaceDataSource(new_path,"SHAPEFILE_WORKSPACE", layername)
if lyr.workspacePath == new_path:
print "Workspace path for " + layername + " is updated."
else:
print "Path not updated for " + layername
mxd.save()
print "Map Saved"
del mxd
The script below will search through all sub-folders from a given root and append the data to newly created shapefiles. Code is commented (you can comment out the print statements). The gist of the workflow is to first get a list of all the ending strings of shapefiles after the first underscore. Then create a unique list based on this list. Use the unique list to create empty shapefiles and append in all shapefiles that end with this unique value.
import arcpy, os
from arcpy import env
env.workspace = r"C:\output"
root_folder = r"C:\test"
# this list will hold the spliced endings of the input shapefiles
ending_list = []
# traverse through all the files and append the endings of the shapefiles to
# the ending_list. Everything after the first underscore will be appended
# for example BL_POINT.shp, BL_POLY.shp, LF_LINE.shp etc
for root, dirs, files in os.walk(root_folder):
for filename in files:
if filename.endswith(".shp"):
underscore_index = [pos for pos, char in enumerate(filename) if char == "_"]
ending_list.append(filename[underscore_index[0]+1:])
# get a unique set of endings
unique_ending_list = list(set(ending_list))
# create an empty feature class for each unique ending with same schema as
# the shapefile for _POINT, _LINE etc
# and Append in all the shapefiles that match the ending
for unique in unique_ending_list:
for root, dirs, files in os.walk(root_folder):
for filename in files:
if filename.endswith(unique):
filepath = root + "\\" + filename
if arcpy.Exists(unique):
print "Appending: " + filepath
print "\tto " + unique
arcpy.Append_management(filepath, unique)
else:
sr = arcpy.Describe(filepath).spatialReference
arcpy.CreateFeatureclass_management(env.workspace, unique, "", filepath, "", "", sr)
print "Appending: " + filepath
print "\tto " + unique
arcpy.Append_management(filepath, unique)
Best Answer
This will do it