Thursday, August 13, 2015

Insert File/Layer Name Into Attribute Table ( The Power Of Phyton Part 5)

Hello,another phyton life saver incominggg

the code im going to post below is very useful if you are going to insert the "file name" of your feature classess into attribute table (in certain field of course)

Here goes

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Script arguments
Wokspace = arcpy.GetParameterAsText(0)
if Wokspace == '#' or not Wokspace:
    Wokspace = "E:\\Data\\Projects\\NWI\\Test_1" # provide a default value if unspecified

Field_Name = arcpy.GetParameterAsText(1)
if Field_Name == '#' or not Field_Name:
    Field_Name = "NAME" # provide a default value if unspecified

Field_Type = arcpy.GetParameterAsText(2)
if Field_Type == '#' or not Field_Type:
    Field_Type = "TEXT" # provide a default value if unspecified

Field_Length = arcpy.GetParameterAsText(3)
if Field_Length == '#' or not Field_Length:
    Field_Length = "70" # provide a default value if unspecified

Expression = arcpy.GetParameterAsText(4)
if Expression == '#' or not Expression:
    Expression = "\"%File Name%\"" # provide a default value if unspecified

# Local variables:
Feature_Class = Wokspace
New_Field_ = Feature_Class
Final_Feature_Class = New_Field_
File_Name = Wokspace

# Process: Iterate Feature Classes
arcpy.IterateFeatureClasses_mb(Wokspace, "", "POLYGON", "NOT_RECURSIVE")

# Process: Add Field
arcpy.AddField_management(Feature_Class, Field_Name, Field_Type, "", "", Field_Length, "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(New_Field_, "NAME", Expression, "VB", "")



this is might be not a well compiled code, but at least this shit is works,!