Find and replace - iterating through a list

Help with writing and playing macros

Find and replace - iterating through a list

Postby sicnarf » Fri Nov 18, 2005 5:49 pm

Hi there.
I have a number of files open, containing commented out #define's.

I also have a list of defines in another file, and for each line in this file,
I want to search through all the open files, finding the commented out #defines that match those in my list.

So I'm trying to loop through my macro to do this, and the RegExp I'm using seems to work fine for the first line of the file but I can't get it to continue round the loop, and onto the next line.

Any ideas?

Top
Loop 0
StartSelect
Key DOWN ARROW
Copy
Paste
Paste
EndSelect
Key UP ARROW
"%//MY_COMMENT ^([ ]++#[ ]++define[ ]++"
Key END
"[~A-Z0-9_]^)"
StartSelect
Key HOME
Cut
StartSelect
Key DOWN ARROW
EndSelect
Key DEL
Find RegExp "^c"
Replace All AllFiles "^1"
EndLoop
User avatar
sicnarf
Newbie
 
Posts: 2
Joined: Fri Nov 18, 2005 12:00 am

Re: Find and replace - iterating through a list

Postby Mofi » Sun Nov 20, 2005 9:16 am

Macro property option Continue if a Find with Replace not found must be enabled. And an exit from the loop is missing.

Here is you macro with little improvements. The first part of the macro avoids an endless loop, if the last line of the file does not end with a line termination.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNum 1
Else
"
"
EndIf
Top
Loop
StartSelect
Key DOWN ARROW
Copy
Paste
Paste
EndSelect
Key UP ARROW
"%//MY_COMMENT ^( ++# ++define ++"
Key END
"[~A-Z0-9_]^)"
StartSelect
Key HOME
Cut
Key DEL
Find RegExp "^c"
Replace All AllFiles "^1"
IfEof
ExitLoop
EndIf
EndLoop

Hope this works, because I could not test it without examples.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find and replace - iterating through a list

Postby sicnarf » Mon Nov 21, 2005 6:03 pm

That did the trick!
Thanks very much for your help.
User avatar
sicnarf
Newbie
 
Posts: 2
Joined: Fri Nov 18, 2005 12:00 am

Re: Find and replace - iterating through a list

Postby eatapeach » Mon May 18, 2009 11:25 am

Hi

I have been trying to adapt this code to meet my requirements - but I can't get the behaviour I want.

I have a file (source file) which lists items I want to delete from a number of other files. It's a straightforward select from the source of the whole line - which might appear in upto 20 files in another directory.

For example these might be in the source file
widget/ABC266
widget/DFF456

and the destination might be
woggle27
woggle56
woggle33
widget/ABC266
widget/DFF456
wengle88
wengle104


so I want to delete the redundant products in the source from multiple other files.

So it seems to be a simple replace in files - however I am struggling to get the code to work. It seems to have no effect - it doesn't seem to select from the original file and thus doesn't delete from numerous target files.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
Loop
StartSelect
Key DOWN ARROW
Copy
EndSelect
StartSelect
Key HOME
Find RegExp "^c"
Replace All AllFiles "^1"
IfEof
ExitLoop
EndIf
EndLoop


Any ideas?
eatapeach
Newbie
 
Posts: 2
Joined: Thu May 14, 2009 11:41 am

Re: Find and replace - iterating through a list

Postby Mofi » Wed May 20, 2009 4:26 am

InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNumGt 1
InsertLine
EndIf

Top
Loop
SelectLine
ReplInFiles Log "C:\Users\Ominous\Desktop\agents\" "*.*" "^s"
""
EndSelect
Key HOME

IfEof
ExitLoop
EndIf
EndLoop

The open file with the list should not be in this directory.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find and replace - iterating through a list

Postby eatapeach » Wed May 20, 2009 11:21 am

OK - thanks Mofi

I used your code and added in
Key DEL
Key DOWN ARROW
StartSelect

so we get

insertMode
ColumnModeOff
HexOff
UltraEditReOn
Top
Loop 0
SelectLine
FindInFiles "C:\Users\Ominous\Desktop\agents\" "" "^s
DeleteLine
Key DOWN ARROW
StartSelect
IfEof
ExitLoop
EndIf
EndLoop

with this result
Search complete, found '' 0 time(s). (0 file(s)).

It's therefore looking for " rather than the text it has selected.

What it is supposed to do is work on a list of 400 items - start at the top, select the first line and delete that line from multiple files in a directory. Then go to the next line, select that one and replace that text with nothing (ie delete any it find) in multiple files.

Where have I gone wrong?
eatapeach
Newbie
 
Posts: 2
Joined: Thu May 14, 2009 11:41 am

Re: Find and replace - iterating through a list

Postby Mofi » Thu May 21, 2009 3:09 am

You can't use FindInFiles for your job because this command creates just a list of found lines with the search string in the output window or an edit window. You want to delete the lines from the files. So you need to use ReplInFiles. I have now created a test scenario on my computer and found out a mistake I have made in my last macro. In my previous post the macro has been corrected - see the bold strings.

Log prevents the appearance of the small info dialog showing you how many lines were deleted in how many files after every Replace In Files operation. You surely don't want to click 400 times on the OK button of this info dialog. Log forces the ReplInFiles command to write the results info into the output window which does not require any user activity. After the macro has finished you see in the output window the result of the last ReplInFiles command which you can ignore.

The currently selected line can be deleted with command DeleteLine or just Delete. But even better is to just unselect the currently selected line and continue on the line the cursor currently is.

The first bold block makes sure that the last line of the list file has a line termination to prevent the macro from running into an endless loop if the last line has no line termination.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros