Find & Replace help

If you need help on how to use the program
Post Reply
daws0n
Posts: 53
Joined: 2005-02-04 13:53:18

Find & Replace help

Post by daws0n »

Hi,
Is it possible to use a single find & replace script to find/replace mutiple different entries?

Right now I am using -

Code: Select all

program FindReplace;

const
  WholeWord = True;
  Search = 'BB01';
  Replace = 'Bathroom Toiletries';
  Field = fieldCategory;
  
var
  s: string;
begin
  s := GetField(Field);
  if WholeWord then
  begin
    if s = Search then
      SetField(Field, Replace);
  end
  else
    SetField(Field, StringReplace(s, Search, Replace));
end.
If I add extra search / replace variables in the const section it complains of duplicate values... Do I need to create different scripts for every find & replace value I specify?

Thanks,
Dawson
antp
Site Admin
Posts: 9720
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

You do not especially have to define consts for that: you can directly put the values in the code instead of using the constants and then duplicate lines which do the work.
Else if you still want to use consts, each have to have different names, and then you have to duplicate code lines below so you repeat them to apply to the different consts.
(not sure if my explanation is clear? :D)
fulvio53s03
Posts: 777
Joined: 2007-04-28 05:46:43
Location: Italy

Post by fulvio53s03 »

With the hope to be useful to you, look at
viewtopic.php?t=4017&highlight=
and especially the procedure replaceWord in the code where I make morel changes in some fields.

Bye. :)
daws0n
Posts: 53
Joined: 2005-02-04 13:53:18

Post by daws0n »

Could you explain a bit more please antp - could I trouble you for an example? :D

Code: Select all

const
  WholeWord = True;
  Search = 'BB01';
  Replace = 'Foam & Creme Baths';
  Field = fieldCategory;
  
const
  WholeWord = True;
  Search = 'BB02';
  Replace = 'Shower Gels and Body Washes';
  Field = fieldCategory;
How do I phrase this properly? How do I define the one consts from the other? I would try writing it in code instead, but I haven't a clue where to start!

Thanks a lot, you have been a big help so far!
antp
Site Admin
Posts: 9720
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Either add new constants:

Code: Select all

program FindReplace;

const
  WholeWordCateg = True;
  SearchCateg = 'BB01';
  ReplaceCateg = 'Bathroom Toiletries';
  WholeWordCountry = True;
  SearchCountry = 'US';
  ReplaceCountry = 'USA';
var
  s: string;
begin
  s := GetField(fieldCategory);
  if WholeWordCateg then
  begin
    if s = SearchCateg then
      SetField(fieldCategory, ReplaceCateg);
  end
  else
    SetField(Field, StringReplace(s, SearchCateg, ReplaceCateg));

  s := GetField(fieldCountry);
  if WholeWordCountry then
  begin
    if s = SearchCountry then
      SetField(fieldCountry, ReplaceCountry);
  end
  else
    SetField(Field, StringReplace(s, SearchCountry, ReplaceCountry));

end.
or directly make the code, keeping only useful stuff, e.g.:

Code: Select all

program FindReplace;
var
  s: string;
begin
  s := GetField(fieldCategory);
  if s = 'BB01' then
    SetField(fieldCategory, 'Bathroom Toiletries');
  s := GetField(fieldCountry);
  if s = 'US' then
    SetField(fieldCountry, 'USA');
end.
daws0n
Posts: 53
Joined: 2005-02-04 13:53:18

Post by daws0n »

I can see what you mean about not using the constants, that would take forever to put in!

Great example there antp, it always makes more sense when someone shows you how it's done :)

Thanks again :grinking:
Post Reply