Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing text in notepad

Status
Not open for further replies.

notacreativeguy

Programmer
Sep 5, 2006
2
US
Hey all, I'm working with Delphi and trying to edit text that's already in a notepad file. I'm not sure why it's not working. Here's my following code, can you tell me what I'm doing wrong?
var testlist: TStringList;
var i: integer;
begin
testlist := TStringList.Create;
if FileExists('P:\myUpgrade.scr') then
begin
testlist.LoadFromFile('P:\myUpgrade.scr');
i := testlist.IndexOf('AAAA');
if i>0 then
testlist := lblNewPrfx1.Caption;
testlist.SaveToFile('P:\myUpgrade.scr');
end;
testlist.Free;
end;

end.
I'm trying to replace 'AAAA' with the contents of the NewPrfx label. Please help.
 
Seems fine to me. Perhaps you could share what is happening when you run it?

I've made some minor changes that may fix it:
Code:
var
  testlist: TStringList;
  i: integer;
begin
  testlist := TStringList.Create;
  try    // always use try..finally constructs
    if FileExists('P:\myUpgrade.scr') then
    begin
      testlist.LoadFromFile('P:\myUpgrade.scr');
      i := testlist.IndexOf('AAAA');
      if i >= 0 then      // indexes start from 0
      begin 
        testlist[i] := lblNewPrfx1.Caption;
        // next line blocked in so only save if chang made
        testlist.SaveToFile('P:\myUpgrade.scr');
      end;
    end;
  finally 
    testlist.Free;
  end;
end;

Perhaps there is no line that equals 'AAAA' exactly. There may be spaces after it or something.
 
What "it's not working" means here?

You can't find a line with the "AAAA" pattern? You find it but the final file is unchanged?

Without knowing the error, I only can guess:

1) The "AAAA" pattern is the first line (index zero) and your code is watching for an index greater than zero: "if i>0 then"

3) You are not finding the "AAAA" pattern due to the fact it is not in a line by itself, you have it in a line like "AAAA some other characters here". Remember that a TStringList line is exactly that: an ENTIRE line from the file.

4) You are not finding the "AAAA" line due to a case-sensitiveness issue.

HTH.
buho (A).
 
Is AAAA a substring of a line that you are looking for? If so, IndexOf will not find AAAA.

Try something along these lines:
Code:
var SL: TStringList;
var i: integer;
begin
SL := TStringList.Create;
SL.Text := Memo1.Text;
for i := 0 to SL.Count - 1 do
        begin
        if Pos('AAAA',SL.Strings[i] > 0 then
                // do whatever
        end;

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top