[GIS] How to merge Shapefiles from multiple folders into Geodatabase Featureclass

arcgis-desktoparcpyesri-geodatabasemergepython

I have a question regarding the merge of a lot of folders, which has a lot subfolders, which has the shapefiles stored. I would like to merge all those in one geodatabase.
So I thought to use a Python script.

import os, arcpy.da

print os.getcwd()

for dirname, dirnames, filenames in os.walk('.'):
    for subdirname in dirnames:
        current_dir =  os.path.join(dirname, subdirname)
        arcpy.env.workspace = current_dir
        fcList = arcpy.ListFeatureClasses(".shp")
        Merge = r"D:\Test\Merge_multiple.gdb\Totals"
        for fc in fcList:
            print fc
            arcpy.Merge_management([fc,destination],Merge)
            arcpy.Delete_management(desination)
            arcpy.Rename_management(Merge, destination)
        break

So, I started the script and after a minute it stops and unfortunately, no result. Because there are several different names in the shapefiles, I thought to use only the suffix .shp.

Could you help me?

[Edit]
I get the path of the working directory in the python window. After this message, the window closes without any errors.
enter image description here

[Edit2]
Based on PolyGeo's advice, I've run the script also in the IDLE environment and this is the result:

enter image description here

Best Answer

To copy shapefiles from multiple folders into a single geodatabase, you could do this:

import arcpy
import os
ws = #path to input folder
dst = #path to output geodatabase
for dirpath, dirnames, filenames in arcpy.da.Walk(ws,datatype="FeatureClass"):
    for file in filenames:
      print file
      filepath = os.path.join(dirpath,file)
      outpath = os.path.join(dst,file[:-4]) #drops the .shp extension
      arcpy.CopyFeatures_management(filepath,outpath)
Related Question