Select to view content in your preferred language

FileNameInsert.py

2495
16
Jump to solution
07-23-2014 12:40 AM
BenLeslie1
Frequent Contributor

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
16 Replies
BenLeslie1
Frequent Contributor

I agree - but it's not clear how you've done that.

0 Kudos
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

0 Kudos
BenLeslie1
Frequent Contributor

Thanks Dan - I'm a tiny step closer to being a python expert.

0 Kudos
JohannesBierer
Frequent Contributor

Haven't seen there was the solution 🙂

It's quite easy, you use the model builder and export to script ....

0 Kudos
BenLeslie1
Frequent Contributor

Sorry I didn't see you also corrected the script - I thought you were just saying your way of displaying the code in GeoNet was better.  It looks like a screenshot of Notepad++ but you can select the text?

I'm pretty new to Arc, I currently find ModelBuilder more confusing than code - I'm used to working with FME a lot so I want it to work like that, I find MB very unintuitive.

0 Kudos
JohannesBierer
Frequent Contributor

It's only a format option in GeoNet, you can define the code as python or other ....

Haven't used FME often, but don't u use there something similar to "model" geoprocessing tasks?

0 Kudos
BenLeslie1
Frequent Contributor

Actually I don't know why I mention it I think they are actually very different beasts and just because the flow diagram layout is similar I want to see ModelBuilder with my FME eyes.  And whereas FME is super flexible, user friendly and powerful, I have so far found modelbuilder to clunky, inflexible and unclear.

0 Kudos