How to insert an increasing number at each "find"?

Help with writing and playing macros

How to insert an increasing number at each "find"?

Postby cqmyg5 » Sat Feb 04, 2006 1:28 am

Hi,

I want a macro with this function:
find "abc" and

for 1st find, add "1" before "abc" to get "1 abc",
for 2nd find, add "2" before "abc" to get "2 abc",
...
for n find, add "n" before "abc" to get "n abc".

please give a hand.
User avatar
cqmyg5
Newbie
 
Posts: 5
Joined: Fri Jul 22, 2005 11:00 pm

Re: How to insert an increasing number at each "find"?

Postby Mofi » Sat Feb 04, 2006 3:13 pm

Is it possible to use the Insert Number feature in column mode?

That would not be so hard to use. It could be done without macro.


If this is not possible here is a macro which could to the job. The UltraEdit macro language does not support variables and mathematical operations. Although I have written and posted macros which increases a number from 1 til 99 with a sequence of IfCharIs commands, I think that in this situation a different and easier method should be used.

The macro first creates a new file and creates in a loop the specified number of empty lines. Modify the red highlighted loop number to specify the amount of numbers you need for your special replace in the source file. Run a find with your search string with Count All before macro execution and you will know how many numbers you will need.

Next it uses the Insert Number feature in column mode to create the numbers. You could modify the green highlighted start number. It's also possible to create the numbers in hex or with leading zeros or with a different step. See options of ColumnInsertNum command.

The following replace is used to collect all numbers in a single line. This is not really needed, if instead of Key DEL the command DeleteLine is used in the macro. But I think it is faster with this method. If you have enabled by default wrapping or only a low maximum number of characters per line then better use the method without the Replace but with DeleteLine.

All preparations are done. The macro now starts the replace loop. In the loop it selects the current number and cuts it to user clipboard 9 and deletes the space (or line if DeleteLine is used here).

Back to main source window and run the replace once. Modify this replace according to your needs. If the string is not found anymore, the temporary file with the numbers is deleted before the loop exits.

If the amount of numbers was not enough, you will get an error message in the temporary file before the loop exits. The cursor in the main source file is then at end of the last replace. Look at the last number, modify the green start number in the macro, delete the temporary file with the error message and run the macro again to continue the replace job from current cursor position in the source file.

Note: The macro always executes the replaces from current position in the main source file at start of the macro!

Finally it clears user clipboard 9 and switches back to the windows clipboard.

Note: This macro needs the macro property Continue if a Find with Replace not found enabled!

InsertMode
ColumnModeOn
HexOff
Clipboard 9
NewFile
Loop 100
"
"
EndLoop
Top
ColumnInsertNum 1 1
ColumnModeOff
TrimTrailingSpaces
Find "^p"
Replace All " "

Top
Loop
SelectWord
Cut
Key DEL
NextWindow
Find "abc"
Replace "^c abc"

IfNotFound
PreviousWindow
CloseFile NoSave
ExitLoop
Else
PreviousWindow
IfEof
"Sorry, amount of numbers was not enough!"
ExitLoop
EndIf
EndIf
EndLoop
ClearClipboard
Clipboard 0
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert an increasing number at each "find"?

Postby cqmyg5 » Sat Feb 04, 2006 7:03 pm

Got it! Admire!
User avatar
cqmyg5
Newbie
 
Posts: 5
Joined: Fri Jul 22, 2005 11:00 pm


Return to Macros

cron