Naming Batch Exports From FGDB

1914
11
09-28-2021 04:51 AM
PaulGardiner
New Contributor III

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).

0 Kudos
11 Replies
DavidPike
MVP Frequent Contributor

Have you modified the cursor to take your fields? The name will be derived from a concatenation of row[0] and row[1].

PaulGardiner
New Contributor III

Apologies, Python scripting is not a forte of mine. Would appreciate it if you could expand a little bit on the above. Thanks. 

0 Kudos
DavidPike
MVP Frequent Contributor

I think it may just be an easy change of the fields specified in the cursor (part of the script), but I'm not completely sure of your field names and the output name, is it based on MapID or fieldValue_photoNumber? 

import arcpy
from arcpy import da
import os

inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)

#data, attname and attachmentid will need to be changed to your relevant fields

with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
    for item in cursor:
        attachment = item[0]
        filenum = "ATT" + str(item[2]) + "_"
        filename = filenum + str(item[1])
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
        del item
        del filenum
        del filename
        del attachment

 

BlakeTerhune
MVP Regular Contributor

Export Attachments Toolbox - Esri Community

I don't see where it would be making multiple folders. There should only be the one fileLocation folder with all the attachments. I altered the code slightly for ArcGIS Pro to make it easier for you to change the file name. Could you give a specific example of how you want the output files to be named and organized?

 

import arcpy
import os

in_table = arcpy.GetParameterAsText(0)
out_dir = arcpy.GetParameterAsText(1)

with arcpy.da.SearchCursor(in_table, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
    for data, att_name, attachmentid in cursor:
        file_name = f"ATT{attachmentid}_{att_name}"
        file_path = os.path.join(out_dir, file_name)
        open(file_path, 'wb').write(data.tobytes())

 

DonMorrison1
Occasional Contributor III

I do something similar to this.  Since I have a relationship defined between the attachment table and the feature class, there is a field in the attachment table that holds the GUID of the associated feature. So my script reads this field in (along with 'DATA', 'ATT_NAME', 'ATTACHMENTID') then looks up the feature to get the desired value ('MapID') in your case.

UriGilad_EsriAu
Esri Contributor

Hi @PaulGardiner,

You can achieve this doing the following workflow:

1. Use the Export Attachments toolbox to export the photos to different folders by field value.

2. In windows explorer, search for the photos (e.g., "ext: jpg") in the folder containing all the exported folders. You will get a list of all the photos from all the folders.

UriGilad_EsriAu_1-1632875610580.png

3. Select all the photos, cut and paste into one folder

4. Delete all the empty folders

The other replies to your post are using the base script provided by Esri (which is also found in my toolbox as Export Attachments From Table). That script doesn't and can't access the field value because that value is not stored in the photo attachment table, but only in the parent Feature Class. To access this value the script needs to match each photo's REL_GLOBALID to the parent's feature class GLOBALID, which is what the Export Attachments toolbox does.

Hope this helps, 

Uri

If this answer solved your question or if you found it helpful please mark it accordingly to help others who have the same question.
PaulGardiner
New Contributor III

Thanks @UriGilad_EsriAu , yes that does work but had wanted to automate the entire process from within GIS. 

Intention was to join the attribute field I wanted to name the files with to the attachment table using GLOBALID/REL_GLOBALID as the join field.

0 Kudos
UriGilad_EsriAu
Esri Contributor

Hi @PaulGardiner,

If it's any help, here's a little ArcGIS toolbox that grabs all the photos from the folders, moves them up a level and deletes the folders. The tool input should be the folder where you exported all your photos to.

It'll work only on jpg and png file types. To add more types open the script and add types extension type in 'file_type' parameter. Any folder that had photos in it and remained empty after the export will be deleted.

Cheers, 

Uri

If this answer solved your question or if you found it helpful please mark it accordingly to help others who have the same question.
PaulGardiner
New Contributor III

Thanks for that, really appreciated. Will try and put together a workflow with both scripts.

0 Kudos