0

I created a Python Script tool to create a GDB inside of a user defined folder path. I am trying to find a way to add another parameter in my script tool for a user to input a shapefile after they created their GDB. The shapefile would be converted into a feature class and stored inside the user created GDB. I have been having trouble making a connection between the shapefile the user enters and then storing it inside the geodatabase.

Code:

#Importing Modules/Creating Function

import arcpy
import os 

#Function to create GDB

def gdbbuilder(gdbnames):
arcpy.AddMessage("...Starting Function")
  if arcpy.Exsists(currentdir):
        arcpy.Delete_management(currentdir)
  else:
      pass
currentdir =  arcpy.GetParameterAsText(0)
projectname = currentdir.split("\\")[-1]
for gdbname in gdbnames.split(";"):
    newgdbname= "{}_{}".format(projectname,gdbname)
    arcpy.management.CreateFileGDB(currentdir, newgdbname, "Current")

#Creating Second & Third User parameter

gdbnames = arcpy.GetParameterAsText(1)
inputlyr = arcpy.GetParameterAsText(2)
arcpy.CopyFeatures_management(inputlyr,currentdir)

My thought process was that the user could create the gdb first then use the inputlyr(shpfile) and the currentdir(gdb) as parameters for the copy feature function to create a featureclass in that gdb. However, I recieve Creating GDB in folder. Traceback (most recent call last): File "C:\Arcpy\GDBcreator.py", line 31, in <module> arcpy.CopyFeatures_management(inputlyr,currentdir) NameError: name 'currentdir' is not defined as the output error.

2
  • 2
    I don't think this will cause that error but the code that you've presented has incorrect indentation after your def statement.
    – PolyGeo
    Aug 18, 2021 at 23:34
  • 1
    You are deleting the existing folder with arcpy.Delete_management(currentdir) but not re-creating it anywhere. Be very careful when using Delete. I have added an answer below. Aug 19, 2021 at 8:03

1 Answer 1

1

I have commented the code below. This will create multiple geodatabases and add the shapefile input to each of them.

import arcpy

currentdir = arcpy.GetParameterAsText(0)
gdbnames = arcpy.GetParameterAsText(1)
inputlyr = arcpy.GetParameterAsText(2)

def gdbbuilder(currentdir, gdbnames, inputlyr):
    arcpy.AddMessage("...Starting Function")

    ## folder name for the project
    projectname = currentdir.split("\\")[-1]
    arcpy.AddMessage(projectname)
    folder_path = currentdir.rsplit("\\", 1)[0]
    arcpy.AddMessage(folder_path)

    ## if the directory already exists, delete it
    if arcpy.Exists(currentdir):
            arcpy.Delete_management(currentdir)

    ## create the folder
    arcpy.CreateFolder_management(folder_path, projectname)

    ## list of gdb names
    gdb_lst = gdbnames.replace("'", "").split(";")

    ## shapefile name without the shp to name the feature class
    ## make sure the feature class is a valid name
    fc_name = inputlyr.split("\\")[-1].replace(".shp", "").replace(" ", "_")

    ## for each gdb in the input gdb list
    for gdbname in gdb_lst:
        ## the name of the gdb to create
        newgdbname = "{0}_{1}".format(projectname,gdbname)
        ## the new gdb path
        gdb_path = "{0}\\{1}.gdb".format(currentdir, newgdbname)

        arcpy.AddMessage("Creating GDB: {0}".format(gdb_path))
        arcpy.CreateFileGDB_management(currentdir, newgdbname)
        arcpy.AddMessage("Converting Shapefile to Feature Class")
        arcpy.FeatureClassToFeatureClass_conversion(inputlyr, gdb_path, fc_name)

gdbbuilder(currentdir, gdbnames, inputlyr)
1
  • Thank you for your help @clubdebambos
    – Ryan Mags
    Aug 19, 2021 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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