Insert string at beginning and end of file

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
JimmyW
Posts: 11
Joined: Sat Oct 01, 2005 9:52 pm

Insert string at beginning and end of file

Post by JimmyW »

I'm converting multiple files to HTML. I want to append <html> to the beginning of the files and </html> to the end. Is there a way to do this? Thanks.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

The regex expressions that should match only at the beginning and only at the end of the document don't work.

Try creating a macro that moves the cursor and makes the insertions. Use <Ctrl+Home> and <Ctrl+End>.
JimmyW
Posts: 11
Joined: Sat Oct 01, 2005 9:52 pm

Post by JimmyW »

Thanks very much. However, is there a way to apply the macro to all open files as I can with the standard search & replace? Better yet, can I have TextPad apply the macro to all files of a certain type in a folder, e.g., *.json?
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

Sounds like you might really want the use of WildEdit.

It can do a Search and Replace using either an Include or Exclude pattern, apply the change to subfolders, and use RE for the search and replace values.
(2[Bb]|[^2].|.[^Bb])

That is the question.
JimmyW
Posts: 11
Joined: Sat Oct 01, 2005 9:52 pm

Post by JimmyW »

Thanks very much. I'm sorry for being dense, but what do you mean by "RE"?
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

Regular Expression
(2[Bb]|[^2].|.[^Bb])

That is the question.
JimmyW
Posts: 11
Joined: Sat Oct 01, 2005 9:52 pm

Post by JimmyW »

Ah, regex, of course! I can't, however seem to find the token or syntax to insert a string at the beginning of file and at the end of a file.

Better yet, is it possible to run a TextPad macro on all open files? Thanks.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

\A or \` matches the beginning of text.
\z or \' matches the end of text.

They work in WildEdit (http://www.textpad.com/products/wildedit/) but, unfortunately, not in TextPad.
JimmyW
Posts: 11
Joined: Sat Oct 01, 2005 9:52 pm

Post by JimmyW »

Thanks again for all the help! The \A works to insert <html> at the beginning, and the \' works to insert </html> at the end. However, using the \Z inserts </html> twice at the end.

I'm just trying the WildEdit demo, so I'm a newbie. When I edit my document, WildEdit creates a new one with -00 appended after the extension. I need to prevent that. Also, I'd like to run both regexes at the same time, i.e., add <html> and </html> at the beginning and end at the same time. I don't see how to do that.

I can create a macro in TextPad to do both at once, but, as I mentioned, I don't see how to run a macro in TextPad against more than one file at a time.
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

Look closely at the times. The -00 files are backups. This way you can do a compare to ensure it really, really did what you want. When you exit WildEdit, it will prompt you and ask whether you want to delete those backup files. You can also delete the backups from the File Menu.
(2[Bb]|[^2].|.[^Bb])

That is the question.
JimmyW
Posts: 11
Joined: Sat Oct 01, 2005 9:52 pm

Post by JimmyW »

Thanks. I haven't exited WildEdit, so I didn't get a prompt. Still, can I do both edits at once, and can I run a TextPad macro on multiple files?
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

It's \z, not \Z.

In WildEdit do both insertions together with
Search for:
(\A)|(\z)

[X] Regular expression

Replace with:
?1<html> [newline]
:?2</html> [newline]

[ ] Literal replacement [i.e., not selected]
I'm not an expert on TextPad macros. I don't use them much because I can't edit them.
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

JimmyW wrote:can I run a TextPad macro on multiple files?
Brute force, yes you can.
Open File1
Run MacroX
Open File2
Run MacroX
Open File3
...

This could obviously get tedious if you needed to run a TP Macro against say a thousand files. In that case, I'd look at using WildEdit, if it's possible, to do what you want. Another option would be a script in whatever you're comfortable with (VBScript, JavaScript, AutoIt, PowerShell, etc.)
(2[Bb]|[^2].|.[^Bb])

That is the question.
Post Reply