mgambone2

List Index Out of Range

Discussion created by mgambone2 on Sep 18, 2012
Latest reply on Sep 20, 2012 by fabl
Hi,

I have a batch processing script for copying multiple shapefiles in a folder into a feature dataset in a file geodatabase.  It's different from the built-in ArcToolBox in that I have to name each input feature class differently from the original names.  So I created a separator in order to split and derive the output feature class name.  The script works when I'm debugging (substituting the argv[..] variables with actual values.  But when I add the script to my Toolbox, I keep getting the error: list index is out of range.  I appreciate any assistance.  The script is as follows:

#---------------------------------------------------------------------------------------
#Script Name: BatchCopyRename.py
#Script Path:
#Created by: Marilyn Gambone
#Date:       9/18/2012
#E-mail: mgambone@brc.tamus.edu

#---------------------------------------------------------------------------------------

#Purpose:
# To batch copy multiple shapefiles in a folder into a file geodatabase feature dataset.

#Deployment
# Attach this script to an ArcToolBox.
# Developed in ArcGIS Desktop license version 10.0.
# Desktop computer Toolbox path:

#Properties
#Name           BatchCopyRename
#Label           Batch Copy and Rename Feature Classes
#Description   Copy multiple Feature Classes from a folder into a file geodatabase
#                   feature dataset and rename each output feature class.

#Parameter List
#           Parameter Properties
#             Display Text                            Data Type                              Type         Direction    MultiValue
# argv[0]  Select Input Features               Feature Layer                           Required   Input         Yes
# argv[1]  Select Output Feature Dataset   Workspace or Feature Dataset    Required   Input         No
# argv[2]  Separator                                String                                     Optional    Input         No
# argv[3]  Output Geodatabase                 Workspace or Feature Dataset   Derived     Output       No



#Import system modules
import ConversionUtils, time

ConversionUtils.gp.Overwriteoutput = 1

#Define message constants so they may be translated easily
msgErrorInvalidOutPath = ConversionUtils.gp.GetIDMessage(86127) #"Output path does not exist"
msgSuccess = ConversionUtils.gp.GetIDMessage(86128) #" successfully converted to "
msgFailed = ConversionUtils.gp.GetIDMessage(86129) # "Failed to convert "


#Set script arguments
# Argument 1 is the list of feature classes to be converted
inFeatureClasses = ConversionUtils.gp.GetParameterAsText(0)

# The list is split by semicolons ";"
inFeatureClasses = ConversionUtils.SplitMultiInputs(inFeatureClasses)

# The output workspace where the shapefiles are created
outWorkspace = ConversionUtils.gp.GetParameterAsText(1)

# Get the separator ('_')
sep = ConversionUtils.gp.GetParameterAsText(2)

# Set the destination workspace parameter (which is the same value as the output workspace)
# the purpose of this parameter is to allow connectivity in Model Builder.
ConversionUtils.gp.SetParameterAsText(3,outWorkspace)

# Set the progressor
#Message "Converting multiple feature classes ..."
ConversionUtils.gp.SetProgressor("step",ConversionUtils.gp.GetIDMessage(86145) , 0, len(inFeatureClasses))

#Loop through the list of input feature classes and convert/copy to feature dataset
for inFeatureClass in inFeatureClasses:
  try:
    # Set the progressor label
    #Message "Converting.."
    ConversionUtils.gp.SetProgressorLabel(ConversionUtils.gp.GetIDMessage(86130) + inFeatureClass)

    # Generate a valid output name
    outFeatureClass = ConversionUtils.GenerateOutputName(inFeatureClass, outWorkspace)
    fcSplit = outFeatureClass.split(sep)
    fcName = fcSplit[2]
    outDataset = outWorkspace + "\\" + fcName.upper()

    # Execute Copy/Convert the inFeatureClasses to the outFeatureClasses
    ConversionUtils.CopyFeatures(inFeatureClass, outDataset)

    # If the Copy/Convert was successfull add a message stating this
    ConversionUtils.gp.AddMessage("%s %s %s" % (inFeatureClasses, msgSuccess, outFeatureClass))

  except Exception, ErrorDesc:
    # Except block for the loop. If the tool fails to convert one of the feature classes, it will come into this block
    #  and add warnings to the messages, then proceed to attempt to convert the next input feature class.
    msgWarning = msgFailed + "%s" % inFeatureClass
    msgStr = ConversionUtils.gp.GetMessages(2)
    ConversionUtils.gp.AddWarning(ConversionUtils.ExceptionMessages(msgWarning, msgStr, ErrorDesc))

time.sleep(0.5)

Outcomes