How To Get Rid of "Copy" from duplicate names

I made some duplicates of notes and moved them to a new container. However, they are all named “Note 1 copy” , “Note 2 copy” etc. What line of action code could I run on the container to get rid of the word “copy”? I’m guessing there will be some kind of If statement involved as well as some kind of wildcard expression that will find the string “copy” within $Name, but that’s as far as I’ve gotten. Please help. Thanks.

OK, when you copy note ‘aa’ and keep duplicating you end up with a sequence like this: aa aa copy, aa copy 1, aa copy 2 … aa copy 9, aa copy 10, etc.

So you want to match all notes where the $Name ends in (reading the matched part left to right, i.e. towards the end):

  • a single space followed by the word ‘copy’: ’ copy
  • and optionally
    • a single space and one or more number characters, that occurring zero or one times: ‘( \d+)?
  • ending at the end of the string, special regex character $ (this avoids a match anywhere but at the end of $Name)

A stamp like this would do the trick, where we match the above and replace it with nothing, i.e. delete the matched part:

$Name = $Name.replace(" copy( \d+)?","");

Want to do this in an agent? OK, then use query:

$Name.contains(" copy *\d*$")

To find the notes, and then run the same action

$Name = $Name.replace(" copy( \d+)?$","");

For my 2¢, I’d not use an agent to do this or you might find your agent is renaming the copies a fast as you are making them. More likely you might duplicate the note and move it elsewhere and then rename. A compromise is to have the agent, but no agent action. The agent finds the note(s) and then when ready you select the child aliases and stamp those manually (as both alias and original have the same $Name).

†. If the note ends with a number you get a different sequences. so if you start with ‘bb8’ you get: bb9, bb10, bb11, etc. The above does not cover this sort of copy name.

‡. Why a different regex? In truth I’m not sure. For whatever reason .contains() doesn’t work if I use the regex that works in .replace()`.

¶. Tinderbox will happily let you have multiple note with the same title (i.e. same $Name and $Path) because they have discrete $ID. But, to do any query or action code based work you’ll need to use $ID as $Name or $Path will always only match the first of all the same-named duplicates. IOW, what looks good on screen in your map (all notes with the same title) may function less well in other aspects of your work.

1 Like

Thanks Mark! I’ll go with the stamp. Just used them for the first time. :blush:

2 Likes