Option for replaceAll()

Hi / Moin!

Is there an option to replace all occurrences with the search an replace action? Like replaceAll() in Javascript?

Thanks,

Thomas

Hi Thomas, welcome to the forum!

Take a look at Search And Replace In A Note – it supports JS regex syntax, so have at it :wink:

Thx! I will try this. It’s a bit complicated to convert ideas for a Python script into actions.

It works for a single note. My idea now would be to search and replace in all notes in one folder. How can I build a loop?

You have several options here. First, you can iterate over all files in your vault, and only work with those whose path start with a particular folder name:

Also, you could use OmniSearch (see How to use Omnisearch for search syntax):

Here, I’ve omitted the “Repeat With” block for brevity, but Search Notes With Omnisearch returns a list of file paths which you can loop through.

HTH!

I tried following:

Th Action runs forever :frowning:

Get List Of All Notes might run into trouble if your vault is extraordinarily large. In this case, the OmniSearch way might be worth a shot.

Does it visibly hang at a particular step, or does it just run forever? If the former: Can you tell at which step it is hanging?

I guess my vault ist very large. The Action ist hanging on the second step.

Yeah, I guess the companion plugin exploded due to overload. I’ll take a note here, this shouldn’t just hang but communicate back.

I recommend the OmniSearch route, then, because it finds the applicable files in Obsidian before handing that list back to AFO, whereas Get List Of All Notes requests a list of all note paths from Obsidian, which seems to cause the problem you’re experiencing.

Thanks for your help. In the meantime I have written a little Python script. Can be started via an shell shortcut :slight_smile:

import re
import os

regex = “- [x].*\n”
vault_directory = PATH_TO_VAULT_AND_FOLDER

txt_files = [file for file in os.listdir(vault_directory) if file.endswith(‘.md’)]
for file in txt_files:
my_file = open(file,‘r’)
content = (my_file.read())
my_file.close()
result = re.sub(regex,“”,content)
my_file = open(file,‘w’)
my_file.write(result)
my_file.close()

Hah, yes. My first impulse was going with sd via shell but I figured since you’ve asked in the AFO support forum, you were looking for a decidedly AFO solution. :wink:

The first intention was to do with AFO for learning purpose. But on the other hand is a Python script a much faster way for me :slight_smile: