FileNameInsert.py

1507
16
Jump to solution
07-23-2014 12:40 AM
BenLeslie1
Occasional Contributor III

I downloaded this arcscript from here:   Filename Insert

It's an old script from 2006 and when I run it I get the following error:

File "C:\FileNameInsert.py", line 15, in <module>

  import win32com.client, sys, string, os

ImportError: No module named win32com.client

How do I modify this script so it works in 2014?

(what I hoping to achieve is to add a filename attribute to my shapefiles so that when I merge them together I can trace back to the originals)

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

again...not tested...try this

import arcpy

# Remember to change this to wherever your shapefiles are stored

arcpy.env.workspace = r"C:\temp2"

fcs = arcpy.ListFeatureClasses()

for fc in fcs:

   print fc

   # Create the new field

   arcpy.AddField_management (fc, "FILENAME", "text", "", "", "50")

   # Apply the filename to all entries     

   arcpy.CalculateField_management (fc, "FILENAME", '"' + fc + '"')

    #print arcpy.GetMessages ()

There seems to be something with the try..except block

View solution in original post

0 Kudos
16 Replies
JohannesBierer
Occasional Contributor III

import arcpy

Then replace gp. through arcpy.

0 Kudos
BenLeslie1
Occasional Contributor III

Thanks - that kind of makes sence but the script isn't doing anything.  It runs without error but doesn't add the new field.

Did you download the script?  Can I ask some questions?

I removed line 17:

          gp = win32com.client.Dispatch...............

was I correct to do this?

I don't understand the 'try:' section....it says:

          try:

               fcs = arcpy.ListFeatureClasses("*", "all")

               fcs.reset()

               rc = fcs.Next()

Why does it define fcs and then reset it on the next line?  What is it doing?

I'll be happy if you can recommend a place where I can read up on these terms.

Regards

Ben

0 Kudos
DanPatterson_Retired
MVP Emeritus

These two lines don't work in arcpy

    fcs.reset()

               rc = fcs.Next()

you might be advised to copy the script to your thread rather than relying on people downloading it themselves.  The translation from 9.x to 10.x isn't simply a matter of replacing gp with arcpy, there are other issues

0 Kudos
BenLeslie1
Occasional Contributor III

Good plan - my Arc machine isn't connected to the internet so usually it's a pain in the neck.  I forgot that I had downloaded the code off the internet.

This is the code after my changes........

#import relevant modules, create geoprocessing dispatch object

import arcpy

#gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")

# Remember to change this to wherever your shapefiles are stored

arcpy.workspace = "C:\temp2"

try:

    fcs = arcpy.ListFeatureClasses("*", "all")

    fcs.reset()

    fc = fcs.Next()

    while fc:

        # Create the new field

        arcpy.AddField_management (fc, "FILENAME", "text", "", "", "50")

        # Apply the filename to all entries      

        arcpy.CalculateField_management (fc, "FILENAME", '"' + fc + '"')

        fc = fcs.Next()

except:

    print arcpy.GetMessages ()

0 Kudos
DanPatterson_Retired
MVP Emeritus

On a quick look (can't test)

arcpy.workspace = "C:\temp2"   #should be "C:/temp2  python uses forward slashes

                                                   # amongst other things, ie raw notation r"C:\temp2

try:

    #fcs = arcpy.ListFeatureClasses("*", "all")

    fcs = arcpy.ListFeatureClasses()           #is all that is needed in your case

    #fcs.reset()         #these two lines don't work in 10

    #fc = fcs.Next()

remove one more fcs.Next line further down

0 Kudos
BenLeslie1
Occasional Contributor III

What shoul I do to correct the while function, it still refers to fc.

I get the following error:

     IndentationError:unindent does not match any outer indentation level

Without the .Next lines how does the code know to iterate through all by shapefiles?

sorry - I still need to be spoon fed this stuff.

0 Kudos
DanPatterson_Retired
MVP Emeritus

arrrgh need arcmap handy

replace the while line with
for fc in fcs:   #keep the proper indentation!

and your homework is to ensure that you have the appropriate syntax in CalculateField management in case it has changed

0 Kudos
BenLeslie1
Occasional Contributor III

Closer....closer....

I'm now getting some results but if I have several shapefiles in c:/temp2 the script only amends the last one (print fc lists all files correctly)

This is my code now:

import arcpy

# Remember to change this to wherever your shapefiles are stored

arcpy.env.workspace = r"C:\temp2"

try:

    fcs = arcpy.ListFeatureClasses()

   

    for fc in fcs:

        print fc

        # Create the new field

        arcpy.AddField_management (fc, "FILENAME", "text", "", "", "50")

        # Apply the filename to all entries      

        arcpy.CalculateField_management (fc, "FILENAME", '"' + fc + '"')

       

except:

    print arcpy.GetMessages ()

0 Kudos
JohannesBierer
Occasional Contributor III

I think this is better than rewriting?

# Import arcpy module

import arcpy

# Set Geoprocessing environments

arcpy.env.workspace = "C:\\temp2"

fcs = arcpy.ListFeatureClasses()

for fc in fcs:

    print fc

    # Process: Feld hinzufügen

    arcpy.AddField_management(fc, "FILENAME", "TEXT", "", "", "100", "", "", "NON_REQUIRED", "")

    # Process: Feld berechnen

    arcpy.CalculateField_management(fc, "FILENAME", "fc", "PYTHON", "")

0 Kudos