I am batch exporting photo attachments from a FGDB using the ExportAttachment script (as attached). The FGDB consists of points which have a field called MapID (which contains a unique identified for each point).
Is there script I can add to this so that the exported photo is named as 'FieldValue_PhotoNo'?
I have also used the Export Attachments tool by @UriGilad_EsriAu but would rather not have to group the photos by field value if possible (so the output consists of a single folder containing all photos rather than multiple folders).
Hey Paul
Not sure if you solved your problem but I have a script that does exactly what you are trying to accomplish
Let me know if you have any questions (in my case I had to filtered for start and end dates but you can remove that)
def main():
import arcpy
from arcpy import da
import os
inTable = "Database Connections\\DATABASENAME@DIRECTCONNECT.sde\\coa_working.COA_GIS.Parcels_point_edit_assessor__ATTACH"
##inTable=arcpy.GetParameterAsText(0)
fc = "Database Connections\\DATABASENAME@DIRECTCONNECT.sde\\coa_working.COA_GIS.Parcels_point_edit_assessor"
##fc = arcpy.GetParameterAsText(1)
fldRelatedInfo = 'PARCELID' # should be valid column in FC
##fldRelatedInfo = arcpy.GetParameterAsText(2)
fileLocation = "d:\\temp\\assessor\\Attch_input"
##fileLocation = arcpy.GetParameterAsText(3)
##print fileLocation
#dStartDate = arcpy.GetParameterAsText(4)
dStartDate = '10/01/2019'
#dEndDate = arcpy.GetParameterAsText(5)
dEndDate = '03/15/2020'
expresDate = "[last_edited_date]>='" + dStartDate + "' and [last_edited_date]<'" +dEndDate + "'"
print expresDate
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## ##Sterp 1:
## ##Query records that are between the selected dates
## print "Entering FC search records between dates"
## ##expresDate = "[PicTaken] = 'Yes' and [last_edited_date]>='" + dStartDate + "' and [last_edited_date]<'" +dEndDate + "'"
## print expresDate
with da.SearchCursor(fc, ['GlobalID', 'PARCELID'],where_clause=expresDate) as cursor:
NewRec = []
multipleVals = {}
rowcountNewRec = 0
for item in cursor:
##Initial counter
##print item[0]
tmf = {item[0]:item[1]}
multipleVals.update(tmf)
NewRec.append(item[0])
rowcountNewRec = rowcountNewRec+1
print len(NewRec)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##Sterp 2:
##Query records from the attached table that matches the previous selection
print "Entering Attachment Searchcursor"
##Search from the attachment table
for itemAttch in multipleVals:
##print "--------------- " + itemAttch + " ---- " + multipleVals[itemAttch]
attExpression = arcpy.AddFieldDelimiters(inTable, 'REL_GLOBALID') + " = '{0}'".format(itemAttch)
print attExpression
with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_GLOBALID'], where_clause=attExpression) as cursor:
filesNew = []
rowcount = 1
for item in cursor:
##Initial counter
relOID = item[3]
print relOID
##Check to see if the selected records in the List created from the date range
if relOID in NewRec:
print "We found a new picture -- " + relOID
else:
print "-- Not relevent"
##print rowcount
##Check to see if the record already has a picture, if it does find the # and assigned the
if relOID in filesNew:
filesNew.append(relOID)
iCount = filesNew.count(relOID)
rowcount = iCount
else:
rowcount = 1
filesNew.append(relOID)
# access related information... Choose Geocode or parcelid????
print "looking for related info"
##myRelatedInfo = QueryRelatedData(fc, fldRelatedInfo, relOID)
myRelatedInfo = multipleVals[itemAttch]
print "****** " +myRelatedInfo
attachment = item[0]
filename = myRelatedInfo + "-0" + str(rowcount) + ".jpg" ##filenum + str(item[1])
print filename
##Save JPG in a folder
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
print "File has been created"
del filename
del attachment
if __name__ == '__main__':
main()
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
should return something like this (PARCELID-XX.jpg) XX=number of attachments)
Thanks all, hoping to get a chance over the coming days to work through your suggestions and will update. Your help, as always, is much appreciated.