Select to view content in your preferred language

EXPORT ATTACHMENTS ArcTOOL per FILE TYPE

144
9
yesterday
Labels (1)
ModernElectric
Frequent Contributor

I found the answer I was looking for previously on mass downloading and organizing attachments from an AGOL Hosted Feature Layer. I am using the Export Attachment geoprocessing tool and it works great.

However, there are a number of different types of attachments, PDFs, images, etc that I have on a variety of different hosted feature layers used in Workforce and Survey123.

Is there a way within the tool, or another tool, to choose which file type I want selected and exported, IE .JPEG versus .PDF? This would help on the export and organization workflow I am building.

0 Kudos
9 Replies
RyanUthoff
MVP Regular Contributor

According to the tool documentation, it honors selections. Would you be able to pre-select all the records based on attachment type, and then use the tool? I haven't personally tried that before, but that's the first thought I had.

0 Kudos
ModernElectric
Frequent Contributor

Yes, the tool does allow for selection using the Select Layer by Attributes. I am using ArcGIS Pro, first building a model to select the records from a Hosted Feature Layer (ie, date range) and then using the Export Attachments tool.

However, unsure how I select the specific attachment type by file type, .JPEG or .PDF that are attached to the selected feature. 

What I want to do is select all the .JPEG images and export those to a specific achieve folder on our network and do a separate export of .PDF attachments and export those to a different separate folder.

0 Kudos
RyanUthoff
MVP Regular Contributor

Well, I was thinking if there was any sort of standardization in the records where you can tell based off of something in the record of what file type it is, you could only select those records and export them. For example, if records that only have a specific attribute "A" are only associated with PDFs, then select all "A" records and export them. But, I totally realize that's probably not feasible. It's more of a long shot idea.

From what it sounds like, you have a single record with multiple file attachment types and want to filter out the attachments that way. Unfortunately in that case, I'm not aware of a way to do that.

0 Kudos
ModernElectric
Frequent Contributor

Appreciate the feedback. Yes, sounds like you understand exactly what I am trying to do. 

Want to be able to write a series of scripts/models to export key attachments and organize into different folders for achieves. If I can figure out how to select certain file types, will save tons of hours of manually organizing the different files. In the future, I can run a batch file that will run the scripts to run a monthly data download, organization, sorting and achieving of field pictures and Survey PDF reports. 

Just need this added step to progress with my design.

0 Kudos
RyanUthoff
MVP Regular Contributor

I don't like this approach, but what you could do is download/export the entire hosted feature service as a FGDB which will give you access to the underlying attachment table. From there, you can filter based on file type in the attachment table. It's certainly not ideal, but it's something. What we really need is query access to the underlying attachment table in AGOL. But without that, I think the options are limited to be able to do anything directly within AGOL.

Edit: I just re-read your original question and saw that you already did this. Sorry! But I'll leave it here in case anyone else comes across this question and finds this comment useful.

0 Kudos
DavidSolari
MVP Regular Contributor

If you can't filter up front, filter out back. Here's a sample that will remove all non-PDF/JPEG attachments from the folder you downloaded them to:

import os
out_dir = r"C:\path\to\folder\with\attachments"
extensions = [".pdf", ".jpeg", ".jpg"]
for root, _, files in os.walk(out_dir):
    for f in files:
        if not any(f.lower().endswith(e) for e in extensions):
            os.remove(os.path.join(root, f))

Feel free to tweak that third line to match your desired results.

0 Kudos
Robert_LeClair
Esri Esteemed Contributor

I think with some Python scripting you should be able to do this - CAVEAT - I have not tested this script as I don't have a feature class with attachments.  Let me know how it goes.

import arcpy
import os

# Set paths
input_fc = r"C:\Path\To\Your\FeatureClass" # Feature class with attachments
output_folder = r"C:\Path\To\ExportedAttachments"

# Make sure output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)

# Get the name of the attachment table
desc = arcpy.Describe(input_fc)
attachment_table = desc.AttachmentTableName

# Check if the feature class has attachments
if not desc.HasAttachments:
raise Exception("This feature class does not have attachments.")

# Create a list of desired file types
valid_extensions = ['.pdf', '.tif', '.tiff', '.docx']

# Create a temporary table view of the attachment table
arcpy.management.MakeTableView(attachment_table, "attachment_view")

# Loop through each extension type and export attachments
for ext in valid_extensions:
# Build SQL query for this file type
where_clause = f"UPPER(NAME) LIKE '%.{ext.strip('.').upper()}'"

# Export matching attachments
arcpy.management.ExportAttachments(
input_fc,
"attachment_view",
output_folder,
"REL_GLOBALID", # Match field
"ATT_NAME",
"DATA",
where_clause
)
print(f"Exported all {ext.upper()} files.")

print("Done exporting attachments.")

DavidSolari
MVP Regular Contributor

This is a fantastic idea unless you read the entire first sentence in the OP, then it's useless for this case.

0 Kudos
ModernElectric
Frequent Contributor

@DavidSolari @Robert_LeClair 

Thank you for both suggestions, big big help. I am in the process of doing some experimentation and will see what works. Will publish my results once I get it figured out.