Search all stored attachments vs linked attachments?

Is there a way to list all stored attachments? And then list all linked attachments?

I didn't find any advanced search functionality for this.
  • edited August 14, 2023
    Assuming you want the full locations of those files, such lists can be generated with code like that below, run within Zotero under Tools\Developer\Run Javascript.

    *All* attachment files in local storage (not just PDFs) are listed with the following code:
    var datadir = Zotero.DataDirectory.dir;
    var sql = "SELECT (? || ? || key || ? || REPLACE(path,?,?)) AS filepathnames FROM itemAttachments JOIN items USING (itemID) WHERE libraryID=1 AND path IS NOT NULL AND path LIKE ? ORDER BY filepathnames";
    var filepathnames = await Zotero.DB.columnQueryAsync(
    sql,
    [
    datadir,
    '\\storage\\',
    '\\',
    'storage:',
    '',
    'storage:%'
    ]
    );
    return filepathnames.join('\n');

    Note that this code is specific to Windows. For Macs/Linux, you should be able to replace \\ with / and get the same result (but someone else would need to test that).

    If you wish to limit the list to just PDFs, use this very slightly modified code:
    var datadir = Zotero.DataDirectory.dir;
    var sql = "SELECT (? || ? || key || ? || REPLACE(path,?,?)) AS filepathnames FROM itemAttachments JOIN items USING (itemID) WHERE libraryID=1 AND path IS NOT NULL AND path LIKE ? ORDER BY filepathnames";
    var filepathnames = await Zotero.DB.columnQueryAsync(
    sql,
    [
    datadir,
    '\\storage\\',
    '\\',
    'storage:',
    '',
    'storage:%.pdf'
    ]
    );
    return filepathnames.join('\n');

    Linked attachments (ie attachments NOT in local storage) can be listed found with the following code:
    var filepathnames = await Zotero.DB.columnQueryAsync('SELECT path AS filepathnames FROM itemAttachments WHERE path IS NOT NULL AND path NOT LIKE ? ORDER BY path','storage:%');
    return filepathnames.join('\n');

    If you have a Linked Attachment Base Directory set, all of the linked file names in the list will be prefixed by 'attachments:' (since that is how they are stored in the Zotero database). You can edit the list to replace the prefix with the LABD folder to get the full path. There is probably a way of getting the LABD in code and listing the full paths that way, but I don't know how to do that.

    If you don't have a LABD set, each linked file's full path will be listed.

    Unfortunately there is no way to save these pieces of code within Zotero for re-use. So just save them in a text file somewhere and copy/paste them each time you need them.
  • @tim820, what sort of modification would be needed to change your scripts so that instead of operating on everything in Zotero storage, it only operates on items in the selected collection?
  • edited March 12, 2024
    @aaaaaaaaaaaaaaa select a collection and then run this modification of the above code for linked attachment files:

    var CurrentCollection = Zotero.getActiveZoteroPane().getSelectedCollection();
    var filepathnames = await Zotero.DB.columnQueryAsync('SELECT path AS filepathnames FROM itemAttachments JOIN collectionItems ON (itemAttachments.parentItemID=collectionItems.itemID) JOIN collections USING (collectionID) WHERE collectionName IS ? AND path IS NOT NULL AND path NOT LIKE ? ORDER BY path',[CurrentCollection.name,'storage:%']);
    return filepathnames.join('\n');

    A similar mod to the other code above should work for non-linked attachments. The same little extra bit of sql has to been added, as well as the collection name variable within the square brackets.




Sign In or Register to comment.