[question] Tags to Folders with Hierarchy? (solved)

edited July 17, 2022
This is a very unique request and probably not possible ... but it never hurts to ask!

I recently migrated several thousand citations from another reference manager, and the way import worked is that what were folders in the other app are now tags in Zotero. I'm wondering if there is any way to make these into folders again? At issue is the fact that many of the tags are like this:

writing/topic name/review
writing/topic name/bibliography
writing/topic name 2/review

etc. Sometimes with many more subfolders than this. I'm finding going through and manually trying to re-create the folder hierarchy and move the items back in to be rather time consuming and just thought I'd see if there are any shortcuts or tools that might help?
  • It could certainly be scripted. That's not something we can help with, but someone in the community might be willing to do this for you for a fee — it would take a bit of custom work.
  • I've done similar work before. I could take a look.
  • Thanks @emilianoeheyns! How to proceed? Let me know what you need from me and how best to contact you. (I couldn't see any DM function here?)
  • You can contact me at emiliano.heyns@iris-advies.com
  • I'd like to thank @emilianoeheyns for helping out. Below is a link to the script, along with some instructions, but first a note:

    I didn't want non-folder tags to be turned into folders, so this only included items with a "/" in the filename. Since I din't use top-level folders I didn't miss anything, but if you store a lot of items in top-level folders in Paperpile this won't catch them. It also will catch a few tags if they have slashes in them, for instance if you have something like "dog/s" as a tag, it will create a folder "dog" and a sub-folder "s". So it worked for me, but might not work for everyone.


    1. Backup, just in case.
    - If sync is enabled, turn it off (after doing a full sync)
    - Save a copy of your Zotero storage folder somewhere. (You can find it by looking in the zotero preferences, under "advanced" > "Files and folders" > "Data Directory Location"

    2. Copy the script from here: https://gist.githubusercontent.com/retorquere/24dfe44b42729034b89257608e1c35a6/raw/be16c11db0a8441787ec35566b81eb837928f1c6/tags2folders.js

    3. In Zotero, go to the Tools -> Developer -> Run Javascript menu, paste the script in the left pane, and click "run". Please leave Zotero alone while it's running; it will show " : assigned" in the right pane when it's done. It will not create any new items, just new folders and assign existing items to those folders.

    4. The initial run will put everything under a folder "!!!TEST!!!" so you can see if the results are what you wanted, if that's the case, you can delete the "!!!TEST!!!" folder (NOT cmd-delete or ctrl-delete: IF IT ASKS YOU WHETHER IT MAY DELETE COLLECTIONS AND ITEMS, CANCEL!. It should ask "Delete collections", that's OK.

    5. Then change the top line from

    const prefix = '!!!TEST!!!/'


    const prefix = ''
    (that is, two single quotes, not a single double-quote)

    and run it again.
  • And don't forget to turn sync back on when done.
  • edited July 17, 2022
    And here is a short script (also by emilianoeheyns) to remove all the tags that have a "/" in the name, if you want to clean up your tags after running the script:

    let tags = await Zotero.Tags.getAll(Zotero.Libraries.userLibraryID);
    tags = tags.map(tag => tag.tag || tag || '').filter(tag => tag.includes('/')).sort()
    tags = tags.map(tag => Zotero.Tags.getID(tag))
    await Zotero.Tags.removeFromLibrary(Zotero.Libraries.userLibraryID, tags)
    return 'done'
Sign In or Register to comment.