Hello everyone,
I am working on a tool that used to input multi feature layers, add _QC to the end of each one and add a QC field for each one of them.
What happened when I add the features for the first time it works and I have all layers exported with correct naming and field.
But the issue happens when I select the layers for the second time, the second selected layers are being exported with no issues but for some reason it delete my first exported layers.
Any idea how to solve this?
The code:
import arcpy
import os
# Get input parameters
input_features = arcpy.GetParameterAsText(0).split(';') # Multivalue input
output_folder = arcpy.GetParameterAsText(1)
# Set overwrite output to True to automatically overwrite existing files
arcpy.env.overwriteOutput = False
# Create a function to export features with QC suffix and add QC field
def export_with_qc_suffix(input_feature, output_folder):
desc = arcpy.Describe(input_feature)
feature_name = os.path.splitext(desc.name)[0]
output_name = f"{feature_name}_QC"
output_feature = os.path.join(output_folder, output_name)
# Check if the output folder exists, and create it if not
if not os.path.exists(output_folder):
os.makedirs(output_folder)
arcpy.CopyFeatures_management(input_feature, output_feature)
arcpy.AddMessage(f"Exported {input_feature} to {output_feature}.")
# Add the "QC" field to the exported feature
output_desc = arcpy.Describe(output_feature)
if "QC" not in [field.name for field in output_desc.fields]:
arcpy.AddField_management(output_feature, "QC", "TEXT", field_length=10)
arcpy.AddMessage(f"Added 'QC' field to {output_feature}.")
# Loop through input features and export them with QC suffix and QC field
for feature in input_features:
export_with_qc_suffix(feature, output_folder)
# Inform the user that the tool has completed
arcpy.AddMessage("Export process completed.")
Note I tried setting arcpy.env.overwriteOutput as True but didn't work as well
Code formatting ... the Community Version - Esri Community
to make reading and comments easier
and you would want to set overwriteOutput to False if you don't want to overwrite what you have done