Batch rename of linked file attachment paths for collection PDFs

I am trying to batch rename the linked file attachment paths for attached PDFs in a Zotero collecton that are broken. The files were located in a directory path that is now defunct. I'd like to batch edit the linked file attachment paths so they point to the new directory where the files now are. Any advice?
  • You can do it by temporarily creating the old directory path (if possible), moving the files there, setting a Linked Attachment Base Directory, moving the files back to where you want them, and then updating the base directory setting.

    If you can't or don't want to do that, the Zutilo plugin can batch-edit linked-file paths.

    Note that the way to avoid this in the future is to always use a Linked Attachment Base Directory.
  • Thanks for the response. Unfortunately I am locked out of the previous directory as I have had to change user profile in MacOS. While I can access the directory as an admin via the Finder and extract individual PDFs, it seems Zotero is blocked from access.

    I have tried the Zutilo plugin. However there seems to be a bug such that when it changes the filename in the linked file attachment path link it also removes the first letter of the filename within the path (!?). For example, a directory/filename path of .../directory/Abcde becomes .../directory/bcde in the new link. Meanwhile the real filename of the PDF file in the directory remains Abcde. This means that Zotero is not able to access the file via the newly created linked file attachment path because the new link has lost the first letter of the filename.

    Any advice welcomed.
  • it also removes the first letter of the filename
    @urbanizationist: This issue with Zutilo has also been reported here:

    Do you remember whether you clicked the "replace all instances" check box? Are you using a Windows setup in addition to your MacOS system?

    In the linked discussion, you can find a script that might help fixing the filenames.
  • It would also help if you could check which version of Zutilo you were using. See "Tools" -> "Add-ons". Did this happen with the latest Zutilo version?
  • I'm using Zutilo 3.8.1 installed today. I don't believe I clicked 'replace all instances'. I do have a version on a Windows machine but I haven't run Zotero on it for over a month.

    I tried using the javascript that you posted on 25th Dec 2020. It runs but doesn't update the attachment path. Though I wasn't clear if it runs as is, or whether I had to replace the generic names (oldfullpath / newfullpath) in the script with my actual pathnames.
  • The script expects that the directories of the selected items are already correct. You can check that with Zutilo's "Show attachment paths" and use "Modify attachment paths" if needed. See my posting from August 27, 2020. Once you've verified the initial paths, you can try using the script to fix the filenames. It checks whether the attachment titles correspond to valid filenames. If so, the new paths will be saved to the database.
  • @urbanizationist: I updated the script. The output is more verbose now.
  • Thanks. I have tried the script but it still doesn't work. While the script output says that the filename has changed to the full Abcde version, the change has not occurred within Zotero's record of the filename which remains bcde and thus there is still no match between the link and the PDF file in the directory. Hence I still get a file cannot be found message when trying to open the file from Zotero. I think I will just need to give up and resign myself to the fact the files are permanently disconnected. Thanks for your help though.
  • The script currently shows "Values for last broken item", but you should realize that the "new full path" is only saved to the database if it is the proper path of a file. If the new filename seems to be the correct one, your problem is most likely that the directory isn't correct. As I wrote above, the directory needs to be corrected before using the script. Use Zutilo's "Show attachment paths" function to see the directory saved in Zotero's database. Then compare that with the actual file directory. See here:

    (If the settings for the Linked Attachment Base Directory are confusing to you, reset it to use absolute paths. This shouldn't be necessary, though.)

    We should be able to help you if you could post the "Values for last broken item" when running the script with one broken attachment selected. In addition, post the value you get when running "Show attachment paths". Finally, also post the actual file path. That should provide all the necessary information.
  • I'm also trying to re-link ZotFile attachments using Zutilo's "Modify attachment paths", but I'm having a slightly different bug to the one experienced by urbanizationist and qqbb.
    In my case, the first character of the file name is not removed, but instead a dot and a space are inserted before the file name.

    The specified path: ... E:\GoogleDrive\REFS\
    Result: ... \directory\. Abcde

    As a result, the modified link does not work properly either.

    I'm using Zotero (on Windows 10), Zulito 3.9.0
  • @hidetonakamura: You could try replacing the initial path "\directory\. " with "\directory\". But please read through

    @urbanizationist, @hidetonakamura: Here's a script for checking properties of your linked attachments that are broken. The script won't modify anything. It will only show you some of your attachment settings so you better understand your current situation. (You need Zutilo to run it.)

    Select a broken linked attachment, go to "Tools" -> "Developer" -> "Run JavaScript", and paste in and run this code:

    // Check properties of linked attachments that are broken
    var basePath = Zotero.Prefs.get('baseAttachmentPath');
    const mode = Zotero.Attachments.LINK_MODE_LINKED_FILE;
    var attArray = ZutiloChrome.zoteroOverlay.getSelectedAttachments(mode);
    var attachmentTitle, attachmentPath;
    attachmentTitle = attachmentPath = '';
    var m = 0;

    for (let i = 0; i < attArray.length; i++) {
    if (await attArray[i].fileExists()) continue
    attachmentTitle = attArray[i].getField('title');
    attachmentPath = attArray[i].attachmentPath;

    var msgCheck = attachmentPath.startsWith('attachments:');
    var msg1 = `attachments: Linked Attachment Base Directory!`;
    var msg2 = `The attachment is saved with an absolute path!`;
    var messageBaseDir = msgCheck ? msg1 : msg2;
    var messageBroken = `\n----\n
    Values for the last broken linked attachment:\n\n
    Attachment title: \n\n${attachmentTitle}\n\n
    Attachment path (saved in Zotero\'s database): \n\n${attachmentPath}\n
    -> This path is broken!\n
    -> ${messageBaseDir}`;

    return `Summary of selected linked attachment items:
    ${attArray.length} item(s) selected
    ${m} item(s) broken\n
    Linked Attachment Base Directory:
    ${basePath ? basePath : 'Not defined. Using absolute paths!'}\n
    ${(m > 0) ? messageBroken : ''}`;
  • Before running the code, you currently need to select "Run as async function". The check box is in the top right of the code window.

    (I created a related PR.)
  • I have run this for one of my collections which has 63 references with attachments. The script found only 21 references and counted 16 of the links broken. I then tried selecting 30 references and it only counted 13 of which 10 were broken. For some reason it doesn't scan all selected references.

    Is there a simpler way I can just tell Zotero to look in the new folder for all attachments? Using the Zotilo file path tool risks corrupting all my filenames through the loss of the first letter.

  • The script above shows you properties of selected linked files. Linked files have a chain symbol in the attachment icon. It seems that some of your broken attachments are actually stored files, which don't have a chain symbol. Please see Stored Files and Linked Files.

    Note that Zutilo won't touch stored files. If stored files are broken, this could be because Zotero's storage directory is not where it was originally (new computer?) or because attachments were, e.g., manually moved out of the storage directory. See also your other thread.

    Also note that Zutilo's "Modify attachment paths" function generally works well. Please follow my explanation here and make a backup before modifying a large number of attachments. If you experience issues, it would help if you could provide steps to reproduce them.
Sign In or Register to comment.