Change one word in all .rtf files in one folder?

Everything related to our flagship word processor.
Post Reply
NisusUser
Posts: 334
Joined: 2011-01-12 05:32:38

Change one word in all .rtf files in one folder?

Post by NisusUser »

Hi, macro mavens & other super users,

Is there way for me to change one word in all NWP (.rtf) files in a certain folder? There are probably close to 200 files in this folder, and the last thing I'd want to do is manually open them all to change one word in each one (some don't even have the word, but I wouldn't know that until I opened them).

Thanks!
Eric
User avatar
phspaelti
Posts: 1345
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change one word in all .rtf files in one folder?

Post by phspaelti »

Hi Eric,
Nisus doesn't have any way to search files without opening them AFAIK. You can use the Finder to search the documents, but it doesn't seem to be very reliable, i.e., it may not find all instances of the word your looking for.

The easiest way to do this using NWP is to just open all the files, and do a Find/Replace on All Open Files. Nisus has a nice display that will show which files have hits and how many, and allow you to jump to any. You can also just do a Find/Replace. When you're done, use <option> Close to close them all. With 200 files, this will give you quite a show, but it should work fine. (This will overwhelm/obliterate your "Open Recent" history :D )
To avoid any mishaps make sure to close all other files before you start this.

One could also write a macro that goes through the files one by one, but the net effect will really be the same. The macro too will have to open each file first to find the word (or not), and then close it again. I guess one thing the macro can do is to open the files without displaying them, unless there is a find hit.
philip
adryan
Posts: 603
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change one word in all .rtf files in one folder?

Post by adryan »

G'day, Eric et al

I would use BBEdit for this. No need to open any of the files. Very fast.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
NisusUser
Posts: 334
Joined: 2011-01-12 05:32:38

Re: Change one word in all .rtf files in one folder?

Post by NisusUser »

Hi, Philip and Adrian,

I really appreciate your help on this. I have not yet had time to try either method, but I will need to do this sometime – soon I think.

I'm not sure how to do it with BBEdit, Adrian, but maybe it's simple if I read up on it. I do have BBEdit on my computer, but I don't recall what I used it for :) Maybe I used it to edit an InDesign script … ?

Thanks to both of you!
Eric
adryan
Posts: 603
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change one word in all .rtf files in one folder?

Post by adryan »

G'day, Eric et al

BBEdit > Search > Multi-File Search…

A window similar to NWP's Find & Replace window is displayed.

In the "Search in" field there you can choose one or more folders. The Replace All operation will affect all files in the chosen folders.

Suppose you wished to change the domain name of a Website in which each page makes mention of its URL. You would set the chosen folder to the topmost folder (ie, the one that contains the entire Website), click on the "Options" button, and tick the "Search nested folders" checkbox.

Tip: Don't bother going to make yourself a cup of coffee while you're waiting for the task to be completed.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
B.Otter
Posts: 30
Joined: 2021-02-06 15:24:00

Re: Change one word in all .rtf files in one folder?

Post by B.Otter »

Thank you, Adrian. Flash!

Brad
NisusUser
Posts: 334
Joined: 2011-01-12 05:32:38

Re: Change one word in all .rtf files in one folder?

Post by NisusUser »

adryan wrote: 2024-10-23 00:32:34 G'day, Eric et al

BBEdit > Search > Multi-File Search…

A window similar to NWP's Find & Replace window is displayed.

In the "Search in" field there you can choose one or more folders. The Replace All operation will affect all files in the chosen folders.

Suppose you wished to change the domain name of a Website in which each page makes mention of its URL. You would set the chosen folder to the topmost folder (ie, the one that contains the entire Website), click on the "Options" button, and tick the "Search nested folders" checkbox.

Tip: Don't bother going to make yourself a cup of coffee while you're waiting for the task to be completed.

Cheers,
Adrian
Adrian,
I finally found the time to get this done with BBEdit. Thank you for teaching me how to do this. It worked great!
Eric
NisusUser
Posts: 334
Joined: 2011-01-12 05:32:38

Re: Change one word in all .rtf files in one folder?

Post by NisusUser »

@adryan
Hello, Adrian (or anyone else who know the answer).

I replaced one word in many files with BBedit. Worked great.

Then I wanted to replace this

Code: Select all

(gim. 1943)
with this

Code: Select all

(1943–     )
(After the digits that is supposed to be an en dash followed by five spaces.)

When I did so, BBedit made the changes (and I accepted them). However, rather than an en dash it put in

Code: Select all

–
So I'm wondering how to get BBedit to actually insert an en dash. If I find out, I think it'll be straightforward enough to replace all of the

Code: Select all

–
instances in all of the files with the desired en dash.

Oh, I did search the internet for this, including a search of the v. 15 BBedit users manual. But I could not find anything.

Thank you for any help you can offer.
adryan
Posts: 603
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change one word in all .rtf files in one folder?

Post by adryan »

G'day, Eric et al

When I tried replacing "(gim. 1943)" with "(1943– )", it worked fine. Of course, ticking the "Grep" checkbox will give a slightly different result, but the en dash still appears as it should.

A search of the Net found a similar discussion at:–

https://forums.livecode.com/viewtopic.php?t=18321

But I couldn't figure out how to do the conversion in BBEdit.

I suggest you contact BBEdit and ask them; I have found them quite helpful in the past. Alternatively, you can post a question on their BBEdit Talk forum. It'd be great if you could let us know here on the Nisus forum what the answer to your problem is.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
NisusUser
Posts: 334
Joined: 2011-01-12 05:32:38

Re: Change one word in all .rtf files in one folder?

Post by NisusUser »

adryan wrote: 2024-11-12 11:57:23 G'day, Eric et al

When I tried replacing "(gim. 1943)" with "(1943– )", it worked fine.
Did you mean when you did the replacement in one file from within Nisus Writer Pro? I think that's what you meant.

Thanks,
Eric
adryan
Posts: 603
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change one word in all .rtf files in one folder?

Post by adryan »

G'day, Eric et al

No, I meant using the Find/Replace mechanism within BBEdit. I had just copied fragments from your posting and pasted them into a new BBEdit document, then applied the substitution.

I've now gone back to using BBEdit's Multi-File Search on a Nisus RTF document. With the "Grep" checkbox unticked, no substitution is made. When the "Grep" checkbox is ticked, the en dash is encoded as "–", as you found. So there's something peculiar about the encoding of en dashes (and some other characters). Some people talk about a clash between UTF-8 and UTF-16.

Try as I might, I could not find a solution. I suggest you contact BBEdit.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
adryan
Posts: 603
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change one word in all .rtf files in one folder?

Post by adryan »

G’day, Eric et al

I tried using a “sed” command in Terminal, but “‚Äì‚Äì” (that’s twice) was inserted (in a Nisus RTF document) instead of the desired en dash.

Then I created a document in TextEdit, saved it as RTF, then ran the Terminal command. This time “–” was inserted (in the TextEdit RTF document) instead of the desired en dash.

I suspect that, unless Nisus offers a multi-file Find & Replace feature for its own RTF files, doing such things with (at least some) non-alphanumeric characters may not be possible.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
adryan
Posts: 603
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change one word in all .rtf files in one folder?

Post by adryan »

G'day, Eric et al

I think I’ve got it!

BBEdit Find field: gim\. 1943
BBEdit Replace field: 1943\\endash

(That’s with your five trailing spaces in the Replace expression.)

Have the "Grep" checkbox ticked. If you have the "Grep" checkbox unticked, you need to omit the backslash in the Find expression and one of the backslashes in the Replace expression.

There is, however, a caveat. If the insertion point is anywhere in the Find expression in a Nisus document, the string will not be found in that document.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
Post Reply