Tuesday, March 31, 2015

Python (ArcPy) - fld2gdb - Exporting Folder of shapefiles to file geodatabase (GDB)?


I have a multiple folder of shapefiles with several shapefiles in each folder. I want to create a geodatabase where the name of feature datasets name should resemble the name of the folders respectively. The shapefiles in each folder should get exported as a Feature classes in Feature dataset. Each feature class name should resemble the shapefile names respectively.



enter image description here

Here is the Python (ArcPy) code....

import arcpy
import os
ShapefilesDirectory = r"I:\python\SampleData\Shapefiles"
arcpy.CreateFileGDB_management("I:/python/SampleData", "Shapefiles.gdb")
arcpy.env.workspace = ShapefilesDirectory
print "Building List of Folder of Shapefiles..."
fldList = arcpy.ListWorkspaces('*','Folder')
print str(fldList)
if len(fldList) == 0:
    print "No folders with shapefiles found"
else:
    for fld in fldList:
        print fld
        arcpy.CreateFeatureDataset_management("I:/python/SampleData/Shapefiles.gdb", fld, "I:/python/MultipleFD2GDB/2229.prj")
        if os.path.exists(Shapefiles.gdb + "\\" + fld) == False:
            print "Creating " + fld + " Directory..."
            arcpy.CreateFolder_management(Shapefiles.gdb + "\\" + fld)
     
     
import arcpy

ShapefilesDirectory = r"I:\python\SampleData\Shapefiles"
# arcpy.CreateFileGDB_management("I:/python/SampleData", "Shapefiles.gdb")
arcpy.env.workspace = ShapefilesDirectory
print "Building List of Folder of Shapefiles..."
fldList = arcpy.ListWorkspaces('*','Folder')
print str(fldList)
if len(fldList) == 0:
    print "No folders with shapefiles found"
else:
    for fld in fldList:
        print fld

No comments:

Post a Comment