Select to view content in your preferred language

Comparing field values to file names

4068
10
06-15-2011 09:09 AM
by Anonymous User
Not applicable
Original User: SStrand

I have a shapefile which has a hyperlink field populated with .jpg files. What I would like to do is have a python script check the hyperlink field against the folder full of .jpg files to make sure that everything will be linking correctly. Any help would be much appreciated!
0 Kudos
10 Replies
by Anonymous User
Not applicable
Original User: rdharles

Can you give an example of what the hyperlink field looks like.
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

It is populated with the path to the .jpg

C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg
C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg
etc etc
0 Kudos
RDHarles
Regular Contributor
Something like this should do it...


import arcpy, os

arcpy.env.workspace = os.getcwd()

# shapefile that contains hyperlink
shp = "myfile.shp"

# Create SearchCursor for feature class.
rows = arcpy.SearchCursor(shp)
for row in rows:
    
    # Get the path to the file from field 'hyperlink'
    path = row.getValue("hyperlink")    

    # Does that file exist?
    if os.path.exists(path):
        print "Link is OK: "+path
    else:
        print "BROKEN LINK!: "+path
    
del rows
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Thank you very much R.D.!
0 Kudos
MikeMacRae
Frequent Contributor
Hey, I'm actually trying to do something very similar. I am trying to compare files names from a field called 'PHOTOS' (a string field containing the files names of photos or jpg's). If the record doesn't have a photo name, it will have the string "N/A") and I want to cross reference those names with the file names in a folder. If they match, I want to copy them to another folder.

The above script works great on my file gdb and I can print out the file names. I went further and tried to test for count, but the count function will not work. I think I have my syntax wrong, but I have honestly spent 2 days on tyhis now with various combinations of code and I am getting different errors every time.....this was easier in 9.3.

import arcpy, os

arcpy.env.workspace = os.getcwd()

# Feature Class that contains PHOTOS field
shp = "Z:\ESRI\Geodatabase\TEST.gdb\point_FC"

# Create SearchCursor for feature class.
rows = arcpy.SearchCursor(shp)
for row in rows:
    # the print path syntax works fine, it's the count that fails....
    path = row.getValue("PHOTOS")    
    print path
    if path != "N/A":
        arcpy.GetCount_management(row)
           
del rows

0 Kudos
by Anonymous User
Not applicable
Original User: rdharles

Mike,
I'm confused, what are you trying to get a count of?
0 Kudos
MikeMacRae
Frequent Contributor
Mike,
I'm confused, what are you trying to get a count of?


I'm trying to get a count of all the records in the "PHOTOS" field that DO NOT have the value 'N/A" in them....
0 Kudos
by Anonymous User
Not applicable
Original User: rdharles

OK.
Here's how to do it:
1.) Do a MakeFeatureLayer
2.) make a selection (SelectLayerByLocation) where PHOTOS != "N/A"
3.) Finally, do the GetCount, but in 10.x, it has to look something like this:
result = str(arcpy.GetCount_management(FILENAME).getOutput(0) )
0 Kudos
MikeMacRae
Frequent Contributor
Hey R.D. Thanks a bunch for the tip. I had used this method earlier but I kept getting error. I had narrowed it down to the !=. The selectbyattributes where clause does not like that operator for NOT EQUALS, which is weird, because I have used it in other SQL queries in python.

After your post, I finally clued in to use <> and that works perfectly. Sometimes it's nice to just have someone jog your memory!
0 Kudos