combining 'create folder' and 'iterate feature classes'

04-09-2014 01:46 AM
New Contributor

I have a folder with different subfolders and I want to copy all the features in it to new created folders with the same structure
I tried as below but then all my features are putted in the same folder
In other words, I have a folder 'X', with 2 subfolders 'Y' and 'Z'. The subfolders contain 3 shapes (T10, T100, T1000). I want under folder 'X', in subfolder 'Y' a new subfolder 'copy' with a copy of the 3 shapes in subfolder 'Y';
in subfolder 'Z' I want a new subfolder 'copy' with a copy of the 3 shapes in subfolder 'Z'; instead of one new subfolder 'copy' under folder 'X' with the 6 shapes.
0 Kudos
1 Reply
Frequent Contributor
I'm not sure of a modelbuilder solution, but there should be a pretty easy way to do this in python.  I wrote out some quick code, if you know python you probably can get it to work for you. 

#Import Modules
import arcpy
from arcpy import env

#Defining Workspace
rootfolder = #Enter Location of folder 'X' here
env.workspace = rootfolder

#Listing all folder in workspace
listfolders = arcpy.ListWorkspaces()

for folder in listfolders:
    env.workspace = folder
    arcpy.CreateFolder_management(folder, "copy")
    listfc = arcpy.ListFeatureClasses()
    for fc in listfc:
        desc = arcpy.Describe(fc)
        name = desc.basename
        arcpy.FeatureClassToFeatureClass_conversion(fc, folder + "/copy", name)

I tested the code and it made a folder named copy in subfolder Y and Z with the shapefiles that were subfolders Y and Z in them(they are still also in subfolder Y and Z)
0 Kudos