Choosing every row of a stringgrid containing this word

I have a stringgrid that could contain something like this in the second column:

folder1
folder1\green.txt
folder2
folder2\folder3\folder4
folder2\folder3\folder4\blue.txt
folder2\folder3\folder4\submit.cgi
folder7\aaa.txt

When a line contains ".cgi", I want "xxx" inserted into the first column of every row containing the same first word before he "\". The first word of the line containing ".cgi" is "folder2" so "xxx" must be placed in all other rows starting with "folder2":

folder1
folder1\green.txt
xxx folder2
xxx folder2\folder3\folder4
xxx folder2\folder3\folder4\blue.txt
xxx folder2\folder3\folder4\submit.cgi
folder7\aaa.txt

I am using Delphi 6.  Your help would be greatly appreciated.
rincewind666Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

twinsoftCommented:
Hi, this does what you want:

call DoWork with your stringlist...

function TForm1.GetField(aFieldPos: Byte; aPacket: String; aDelimeter: String = '\'): String;
var
 i: Byte;
begin
 for i := 1 to aFieldPos - 1 do
  aPacket := Copy(aPacket, Pos(aDelimeter, aPacket) + 1, Length(aPacket));
 Result := Trim(Copy(aPacket, 1, Pos(aDelimeter, aPacket) - 1));
end;

procedure TForm1.DoWork(ts: TStringList);
var
 i, x: Integer;
 s: String;
begin
 with ts do
  for i := 0 to Count - 1 do
   if (Copy(Strings[i], 1, 3) <> 'XXX') and
      (Pos('.cgi', Strings[i]) > 0) then
    begin
     s := GetField(1, Strings[i]);
     for x := 0 to Count - 1 do
      if GetField(1, Strings[x]) = s then
       Strings[x] := 'XXX' + Strings[x];
    end;
end;
rincewind666Author Commented:
I am getting this error:

Incompatible types: 'TStringList' and 'StringGrid'

I am a beginner and may be missing something.
twinsoftCommented:
Hi, you shoud call it like this

DoWork(aStringGrid.Cols[1]); // 1 for the second column
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

rincewind666Author Commented:
Sorry but I am still getting the same error:
Incompatible types: 'TStringList' and 'StringGrid'
twinsoftCommented:
You doing something wrong...

Please send me the line of code where you call DoWork...

rincewind666Author Commented:
DoWork(ChmodGrid1.Cols[1]); // 1 for the second column
twinsoftCommented:
Hmm, make the following change in DoWork:

procedure TForm1.DoWork(ts: TStrings);
rincewind666Author Commented:
No more error.

However all the rows now display "XXX".  I only need the row containing ".cgi" and its first word before the "\" to display the XXX.

I appreciate your help with this.
twinsoftCommented:
Hi, this code works fine with me...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure DoWork(ts: TStrings);
    function GetField(aFieldPos: Byte; aPacket: String; aDelimeter: String = '\'): String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function TForm1.GetField(aFieldPos: Byte; aPacket: String; aDelimeter: String = '\'): String;
var
 i: Byte;
begin
 for i := 1 to aFieldPos - 1 do
  aPacket := Copy(aPacket, Pos(aDelimeter, aPacket) + 1, Length(aPacket));
 Result := Trim(Copy(aPacket, 1, Pos(aDelimeter, aPacket) - 1));
end;

procedure TForm1.DoWork(ts: TStrings);
var
 i, x: Integer;
 s: String;
begin
 with ts do
  for i := 0 to Count - 1 do
   if (Copy(Strings[i], 1, 3) <> 'XXX') and
      (Pos('.cgi', Strings[i]) > 0) then
    begin
     s := GetField(1, Strings[i]);
     for x := 0 to Count - 1 do
      if GetField(1, Strings[x]) = s then
       Strings[x] := 'XXX' + Strings[x];
    end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 DoWork(StringGrid1.Cols[1]);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
 StringGrid1.Cols[1].Add('folder1');
 StringGrid1.Cols[1].Add('folder1\green.txt');
 StringGrid1.Cols[1].Add('folder2');
 StringGrid1.Cols[1].Add('folder2\folder3\folder4');
 StringGrid1.Cols[1].Add('folder2\folder3\folder4\blue.txt');
 StringGrid1.Cols[1].Add('folder2\folder3\folder4\submit.cgi');
 StringGrid1.Cols[1].Add('folder7\aaa.txt');
end;

end.


********************* dfm file ******************

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 40
    Top = 76
    Width = 413
    Height = 217
    FixedCols = 0
    RowCount = 12
    TabOrder = 0
    ColWidths = (
      64
      160
      64
      64
      64)
  end
  object BitBtn1: TBitBtn
    Left = 44
    Top = 12
    Width = 75
    Height = 25
    Caption = 'BitBtn1'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
end

Open in new window

rincewind666Author Commented:
We are getting there but using this code I get this in Col 1

folder1
folder1\green.txt
folder2
XXXfolder2\folder3\folder4
XXXfolder2\folder3\folder4\blue.txt
XXXfolder2\folder3\folder4\submit.cgi
folder7\aaa.txt

The XXX should be in Col 0 and should include all instances of the first word, which includes the row with folder2 on its own.

I am a beginner with Delphi so I do not know if this involves much code adjustment.  I really do appreciate your help.  I think e are nearly there now. Thanks.
twinsoftCommented:
Ok, change DoWork like this:

procedure TForm1.DoWork(c0, c1: TStrings);
var
 i, x: Integer;
 s: String;
begin
 with c1 do
  for i := 0 to Count - 1 do
   if (Copy(c0.Strings[i], 1, 3) <> 'XXX') and
      (Pos('.cgi', Strings[i]) > 0) then
    begin
     s := GetField(1, Strings[i]);
     for x := 0 to Count - 1 do
      if Pos(s, Strings[x]) > 0 then
       c0.Strings[x] := 'XXX';
    end;
end;

and call it like this

 DoWork(StringGrid1.Cols[0], StringGrid1.Cols[1]);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rincewind666Author Commented:
Excellent!.  It works OK now. Many thanks for your help which is greatly appreciated.
rincewind666Author Commented:
Excellent!.  It works OK now. Many thanks for your help which is greatly appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.