Deleted.

4829
3
Jump to solution
08-01-2012 05:23 AM
JoshTownsend
New Contributor II

Deleted

0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
0 Kudos
PeterWilson
Occasional Contributor III
thanks Mathew, the following was helpful.

Regards
0 Kudos
curtvprice
MVP Esteemed Contributor
Here's a fixed version of Josh's code...

import win32com.client, sys, string, os
gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")
gp.workspace = " ...  "
try:
    fcs = gp.ListFeatureClasses("*", "all")
    fc = fcs.Next()
    while fc:
        gp.AddField_management (fc, "FILENAME", "TEXT", "", "", "50")
        fcName = os.path.splitext(fc)[0]
        gp.CalculateField_management (fc, "FILENAME", fcName)
        fc = fcs.Next()
except:
    print gp.GetMessages ()


For the archive, the above is ArcGIS 9.2 syntax, and requires PythonWin to be installed (for the win32com import).

For people who are at 10.0 or later, the way to do this would be something like this.
Note I modified Calculate Field to use the Python parser instead of VBScript.

import arcpy
arcpy.env.workspace = " ...  "
try:
  fcs = arcpy.ListFeatureClasses("*", "all")
  for fc in fcs:
    arcpy.AddField_management(fc, "FILENAME", "TEXT", "", "", "50")     
    fcName = os.path.splitext(fc)[0]
    arcpy.CalculateField_management(fc, "FILENAME", "'{0}'".format(fcName), "PYTHON_9.3")
except:
  print arcpy.GetMessages()


[thread=48475]Please read: how to post Python code[/thread]

I would be remiss not to mention that this sort of thing is also pretty easy to do with ModelBuilder 10.0 iterators.
0 Kudos