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?
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.
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.
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.
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.
https://forums.zotero.org/discussion/comment/363117/#Comment_363117
(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.
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 5.0.96.2 (on Windows 10), Zulito 3.9.0
@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
m++;
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 : ''}`;
(I created a related PR.)
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.
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.