Batch edit entries

I have multiple entries that feature the same field content. It's an archive that has changed name, and I need to update the name. As there are hundreds of entries, I'd rather not do this one by one, but as a single 'search and replace' operation.

Is that doable directly in Zotero?

Is that doable manually in the database and what does it take for that?

Thanks for your help!
«1
  • It's not currently possible but planned for the next major release (i.e. not the 3.0.12 due out in the next week or so, but the 3.1/3.5 release).

    Doing that directly in the database would require running an sqlite client. Unless you know what you're doing in sqlite, that's not recommended.
  • You can also use the Javascript API, http://www.zotero.org/support/dev/client_coding/javascript_api#examplebatch_editing
    with:
    var fieldname = "archive";
    var oldValue = "Old Name";
    var newValue = "New Name";
  • @Gracile Thanks, that looks very promising, will give it a shot!
  • @Gracile Worked like a charm, thank you very much!
  • Hi,

    I need to search author names such as "Charles S Prebish", without a dot after middle initial, and replace with "Charles S. Prebish" with a dot after middle initiale.

    What fieldName should I enter in the Javascript API?
    var fieldName="";
    var oldValue = "Charles S Prebish";
    var newValue = "Charles S. Prebish";

    Would this code line works?
  • You can't batch edit author names with this code.
  • using creators.firstName (and just Charles S) wouldn't work?
  • Here is what I've tried:


    var fieldName = "creators.firstName";
    var oldValue = "Charles S.";
    var newValue = "Charles S";

    var fieldID = Zotero.ItemFields.getID(fieldName);
    var s = new Zotero.Search;
    s.addCondition(fieldName, 'is', oldValue);
    var ids = s.search();
    if (ids) {
    for each(var id in ids) {
    var item = Zotero.Items.get(id);
    var mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
    item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
    item.save();
    }
    alert(ids.length + " items updated");
    }
    else {
    alert("No items found");
    }


    Result is:
    Invalid condition 'creators.firstName' in hasOperator()
  • sorry, I was guessing/asking - if you look at the supported fields it's clear that this won't work. Creators have their own syntax via getCreator (you can see it used in some of the examples further up on that page) and that'd get a bit more involved, though if you know what you're doing in javascript a bit it doesn't look prohibitively hard.
  • edited March 4, 2013

    Creators have their own syntax via getCreator (you can see it used in some of the examples further up on that page)


    What page is it? Can you give me the link?
  • edited March 4, 2013
    just further up on the js api page, e.g. here: http://www.zotero.org/support/dev/client_coding/javascript_api#adding_items_and_modifying_data

    I haven't thought this through, but since implementing the search via API looks like it would take a bit of code, you could just do the search in Zotero, create a saved search, and then just work with items in the saved search, loop through the creators in that and change the first name where applicable. I haven't worked with the js api so the exact syntax would take me some time to figure out, too, so unless Gracile knows this right away you're likely on your own with that.

    edit: btw. you can use the <blockquote> tag for quotes.
  • @francoisthibeault: what adamsmith said... I'd be happy to help you on that but I don't have time for now.
  • edited March 5, 2013
    @adamsmith
    I'm not a programmer myself... I'm editing my Ph. D. thesis so that it is conform to my university style. I have hundreds of references to check and edit, and a search/replace function would have been essential to this task.

    The Execute JS API can be used to edit data in the Zotero Advanced Research pop-up window only (with the API, you choose whatever Firefox window you want to work with). How can I use the API at this point?

    @gracile
    What then would be the final code, using
    http://www.zotero.org/support/dev/client_coding/javascript_api#adding_items_and_modifying_data

    and the JS search/replace loop above?
  • @adam smith:

    you mentioned this function would be implemented with a new release of Zotero. Is this still to come? Or have I missed something in the menu on how to get this done?
  • Devs have projected this for Zotero 4.2 in the best case, more likely the version after. So no, you haven't missed anything.
  • How to find all empty language fields?
  • advanced search: language --> does not contain --> %
  • Thanks, but I mean do this in a script above. What oldValue variable must be for choosing all empty "language" fields.
  • edited April 27, 2014
    an empty string ("") should work. Have you tried?

    edit: or otherwise - work with the search condition - i.e. set the old Value to % and the search condition operator to 'does not contain'
  • Invalid operator 'does not contain' for condition language
    and for empty string: No items found. Any suggestions?
  • sorry, no spaces, that should have been 'doesNotContain'
    I'm not 100% sure the wildcard will work, but I think it will.
  • Error: '87' is not a valid field for type 14

    I set:
    var fieldName = "language";
    var oldValue = "%";
    var newValue = "en";
  • it's trying to set a language field for an item that doesn't have language - i.e. an attachment and a note. I think you should just be able to exclude those two in search conditions, soemthing along the lines of "itemType", isNot, "attachment" but you'll likely have to play around with this some.
  • I understand. Thank you very much again
  • FYI, using the wildcard ("%") and 'doesNotContain' to find empty fields returns results much faster for large libraries than searching for an empty string ("") and 'is'. Not exactly sure why, but it is my consistent experience.
  • I am trying, for the very first time, to use a script to batch edit. I do not understand the instructions here and at https://www.zotero.org/support/dev/client_coding/javascript_api#adding_items_and_modifying_data

    I want to change blank language fields to en. I have the .js file ready (https://gist.github.com/zuphilip/b942c8bf2f1126982517) I have saved a search with all my Lang=blank items in it and have that open in Zotero Standalone. I have installed ExecuteJS, invoked it in Firefox, pasted in my code. The next instruction is

    "In Execute JS, switch the target window to an open browser window, paste the relevant code into the “JS-Code to execute” box, make any necessary changes, and click “Execute”. "

    Any old browser window will do? OK, choose a Google tab. Execute. And I get "TypeError: this.collectionsView.selection is undefined." I'm actually quite relieved nothing worse happened, as this process makes no sense to me. There seems to be a big hole in the instructions that is probably painfully easy to anyone who has done this before, but I have not. Can anyone give me a step-by-step on batch editing? Thanks!

    Zotero Version 4.0.29.10
  • edited February 6, 2017
    @Sopdet
    Short answer: that can't work with Zotero Standalone.
  • From your response, I gathered that it should, in contrast, work with Zotero online. Therefore, the missing information that should be added to the instruction page is:
    "In Execute JS, switch the target window to a Zotero browser window with the relevant records selected, paste the relevant code into the “JS-Code to execute” box, make any necessary changes, and click “Execute”. "
    It may also be useful for others who follow this path to know that JS Execute installs in Firefox without a visible trace. In order to invoke it, you have to show menus and find it in your tools menu.
    The batch edit code now works perfectly.
  • (Not "Zotero online", Zotero for Firefox.)
  • @dstillman Any chance of Standalone getting a JavaScript console or compatibility with Execute-JS before the Electron transition?
Sign In or Register to comment.