I am working with Korean topo map layers. They are 1:1000 scale and divided by each map section.

enter image description here

they are also stored in different folder.

Since a layer shows road has same name (e.g., a0010000_polygon.shp), i added the district name as a prefix (e.g., 376120547_blah blah_..._a0010000_polygon.shp)

Here is the problem. now, i need to merge each layers that have same suffix (i.e., merging all layers that ends with a0010000_polygon.shp in each folder) to make one large big huge layer

What i did is, iterate thru all the folder and using wildcard and make feature layer (add to the TOC). Then, drag the layers to merge toolbox..

enter image description here

there are at least 180 layers in each folder, and doing 180 times of this is kinda crazy.

Are there better way to solve this issue?

I came up with this model ... (using collect value) but still i need to adjust wildcard to iterate thru layers have same suffix :(

enter image description here

Oct. 16, 2016 I add this comment

my final (?) model looks like this. (it ain't final until it really works) what i did is that i create a table that contains all possible layers (See 'library' field) enter image description here

then i iterated thru the table row and get the row value and put that in my model.. so that it merges all layers that have same name

enter image description here

the problem is that the shape layers in a folder doesn't have all layers in the library table (e.g., the library has all possible layers, which is 108. but shape layers in a folder has only 40 or sometimes 60) and therefore, it returns merging error lol

so i was thinking about using try / except python script and put that in a model check this --> [http://resources.esri.com/help/9.3/arcgisdesktop/com/gp_toolref/automating_your_work_with_models/branching_colon_implementing_if_then_else_logic.htm][5]

spent almost a week to solve this issue with modelbuilder and it has some limit i think. I will come up with better idea if i can. (so far the best way / easiest way is insert arcpy script try/except) but if there is anybody who knows more precise and elegance way to solve this, please share with us.

after i tried 'dru87''s code, i finished the work. I typed line by line into the console and run and it only returned the last string in the list.

instead, you must copy and paste the entire code and run it (of course, you need to change the workspace and stringSearch)

awesome. and nice. still i have the modelbuilder here and i prefer modelbuilder because i suck at python and if anybody can improve the model, please let me know!

  • 2
    I think the better way to solve this is to use ArcPy but if you are wedded to ModelBuilder then perhaps investigate using a sub-model within a model so that you can have an iterator within each.
    – PolyGeo
    Commented Oct 12, 2016 at 2:06
  • I'm willing to use ArcPy. Which keyword should i google ?
    – Pil Kwon
    Commented Oct 12, 2016 at 3:02

1 Answer 1


As PolyGeo already suggested, using ModelBuilder for such a task is a bit messy. I had similar problems before, which I solved using arcpy. I adapted my script to your needs - try using this code in your command window in ArcCatalog (after setting your paths and search strings of course):

import arcpy, os

## 1. Set work environment and search string for shapefiles
workspace = r"C:\Users\sutter\Desktop\GIS_temp\stack_test"
## Put all your searchStrings in this list, separated by commas
searchStrings = ["*prefix*", "*.shp", "*stringXY*"]
outnameOmit = "*"

arcpy.env.workspace = workspace
arcpy.env.scratchworkspace = workspace
arcpy.env.overwriteOutput = True

for string in searchStrings:
    ## 2. Search workspace and subdirectories for feature classes that contain the search string and put them into a list called listShps:
    listShps = []
    for path, subdirs, files in os.walk(workspace):
        arcpy.env.workspace = path
        files = arcpy.ListFeatureClasses(string)
        for fc in files:
            listShps.append(os.path.join(path, fc))
    arcpy.env.workspace = workspace

    ## 3. Get feedback of how many feature classes were found:
    print u"A total number of {0} shapefiles with the search string {1} were found and added to the processing list".format(len(listShps), string)

    ## 4. Process the listed shapefiles:
    if listShps:
        for char in outnameOmit: string = string.replace(char, "")
        outName = u"{0}".format(string)
        outShp = u"{0}\{1}_merged.shp".format(workspace, outName)
        arcpy.Merge_management(listShps, outShp)
        print u"Merge for {0} complete.".format(string)
        print(u"No shapefiles match the search string {0}, merge operation skipped.".format(string))

print(u"All search strings processed. Script finished successfully.")

Let me know if this wasn't what you're looking for.

  • I have a suffix and i put searchString as
    – Pil Kwon
    Commented Oct 13, 2016 at 10:12
  • searchString = u"*a0010000_line.shp and it says... A total number of 0 shapefiles with the search string *a0010000_line.shp were found and added to the processing list
    – Pil Kwon
    Commented Oct 13, 2016 at 10:12
  • which folder did you set as your workspace? it needs to be the one that contains all the subfolders and shapefiles. the code should also work for suffixes, just put the *-character at the right spots. i tested the script again, works fine for me. could the korean characters possibly cause any problems?
    – dru87
    Commented Oct 13, 2016 at 11:12
  • 1
    oh, awesome. it worked! here's the same issues still. there are more than 180 layers. if i use the for statement, and change "searchString", you think all layers can be merged ??
    – Pil Kwon
    Commented Oct 13, 2016 at 13:28
  • 1
    oh i have to put the entire code together, not line by line!!!
    – Pil Kwon
    Commented Oct 18, 2016 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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