0

I have hundreds of shapefiles that are stored in separate folders, which are within several other folders. Among the shapefiles I only need the ones that are named 'LAP010'.

How do I get that specific shapefile, merge them all together, then clip?

I know how to do it in ModelBuilder using an iterator, but I need to do this process 175 times and creating a script would be much more ideal in this situation. Currently in the model I have Iterate Feature Classes -> Collect Values -> Merge -> Clip. This works, but only 1 shapefile at a time. Unfortunately I do not have arc on my home computer and I am not able to access the normal internet from my work computer. So any help would be great.

I'm new to python, so any help and being able to break it down to a super simple understanding would be awesome.

Runing arcgis. 10.2

closed as off-topic by PolyGeo Mar 2 '16 at 2:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What have you researched? Do you have any code so far? Model Builder can export out python code, so I would probably build it in Model Builder and export the python and then make your changes that you need. – ed.hank Mar 2 '16 at 1:19
2

Here's a start:

import os, fnmatch, arcpy

# Enter the path where you want your output feature classes placed. Be sure that
#   if this isn't a geodatabase that you append '.shp' to your output feature
#   class names below
arcpy.env.workspace = r'C:\temp.gdb'
fcMerged = 'MyMergedFeatureClass'
fcClipped = 'MyClippedFeatureClass'
fcToClip = r'C:\TheFeatureClassToWhichIWantToClipTheData.shp'

searchString = 'LAP010.shp'
# An empty list to store the full paths to the matching shapefiles
filesMatched = list()

# This recursively searches the directory and its subdirectories
# In the parentheses, enter the path to the root directory at which you wish to
#   start your search
for root, subfolders, files in os.walk(r'C:\Flurbington'):
    # This finds files in the current directory that match your search
    for f in fnmatch.filter(files, searchString):
        # This adds each matching file's full path to your list
        filesMatched.append(os.path.join(root, f))

# Merge the files
arcpy.Merge_management(filesMatched, fcMerged)
# Clip the files
arcpy.Clip_analysis(fcMerged, fcToClip, fcClipped)

If you have any questions, let me know.

  • @ Tom - You rock, thanks man. Just a quick question for the searchString = 'LAP010.shp'. Since a shapefile consists of 7 files, will that matter when extracting LAP010? Also, I would totally send you edible arrangements if I could. You are my hero. – Brian J Mar 2 '16 at 1:25
  • +1 Nice use of walk and fnmatch. – Aaron Mar 2 '16 at 2:07
  • @BrianJ Yes. That's why I used the .shp extension in the search query. Arc refers to shapefiles by that name, so that's all you need. You could alternatively walk the directories and use arcpy.ListFeatureClasses, but this demos a more general file search that you can use in numerous other instances. – Tom Mar 2 '16 at 2:50

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