Implementation Code for One-Click Conversion Between MDB and GDB in ArcGIS

#-*- coding: utf-8 -*-
# This script can be used for conversion between GDB and MDB
import arcpy
import os
print("Please enter the database")
SRSJK = r'D:\工作\数据处理\20250616\Project360702.gdb'
clean_path = SRSJK.rstrip(os.sep)
print(clean_path)
srsjkhzm = os.path.splitext(clean_path)[1]
print("Database entered, the type of database you entered is " + srsjkhzm)
print("Please enter the folder for the output database")
SCSJKLJ = r'D:\工作\数据处理\20250616'
print("Output database storage folder entered")
print("Please enter the output database type 'MDB or GDB'")
desc = arcpy.Describe(SRSJK)
srsjkmc = desc.name
SCSJKLX = "MDB"
if SCSJKLX == "MDB":
    SCSJK = arcpy.CreatePersonalGDB_management(SCSJKLJ, srsjkmc, "10.0")
    SCSJK = SCSJK.getOutput(0)
elif SCSJKLX == "GDB":
    SCSJK = arcpy.CreateFileGDB_management(SCSJKLJ, srsjkmc, "10.0")
    SCSJK = SCSJK.getOutput(0)
arcpy.env.workspace = SRSJK
datasets = arcpy.ListDatasets()
print("Copying datasets")
for dataset in datasets:
    srsjjlj = os.path.join(SRSJK, dataset)
    scsjjlj = os.path.join(SCSJK, dataset)
    print("Copying dataset: " + dataset)
    arcpy.Copy_management(srsjjlj, scsjjlj)
print("Dataset copy completed")
featue_classes = arcpy.ListFeatureClasses()
print("Copying feature classes outside of datasets")
for featue_class in featue_classes:
    srysllj = os.path.join(SRSJK, featue_class)
    scyslj = os.path.join(SCSJK, featue_class)
    print("Copying feature class outside of datasets: " + featue_class)
    arcpy.CopyFeatures_management(srysllj, scyslj)
print("Feature class copy completed")
print("Copying database tables")
tables = arcpy.ListTables()
for table in tables:
    print("Copying database table: " + table)
    scblj = os.path.join(SCSJK, table)
    arcpy.Copy_management(table, scblj)
shange = arcpy.ListRasters()
print("Database table copy completed")
print("Copying database rasters")
for sh in shange:
    print("Copying database raster: " + sh)
    scsglj = os.path.join(SCSJK, sh)
    arcpy.Copy_management(sh, scsglj)
print("Database raster copy completed")

Leave a Comment