tyfing
asked on
Replacing text in txt files
I have my data saved as C:\data.txt and this is the data
Peter
Level=02
Panel=02
John
Level=02
Panel=02
how can i change the Peter 's Level's '02' to '03' and save the changes ?
I want my button to 'look' for the 2 digits allocated to 'Level' that is tagged under 'Peter' in data.txt, change it to '03' and save the changes.
Peter
Level=02
Panel=02
John
Level=02
Panel=02
how can i change the Peter 's Level's '02' to '03' and save the changes ?
I want my button to 'look' for the 2 digits allocated to 'Level' that is tagged under 'Peter' in data.txt, change it to '03' and save the changes.
ASKER
i do not get wat u mean
HI,
Put it more clearily (for the second proposition).
1.Read your file to TStringList object (call it YourStringList).
2.Using IndexOf method of YourStringList find the index of the string "Peter".
3.The next index in YourStringList has then the string "Level=02".
4.Change in that string the last Char from "2" to "3".
That's all.
Sincerely,
Nestorua.
Put it more clearily (for the second proposition).
1.Read your file to TStringList object (call it YourStringList).
2.Using IndexOf method of YourStringList find the index of the string "Peter".
3.The next index in YourStringList has then the string "Level=02".
4.Change in that string the last Char from "2" to "3".
That's all.
Sincerely,
Nestorua.
tyfing,
Copy/Paste (place Button1 on the form):
---------------
function ReplaceIt(FileName, HeaderString, ValueString, NewValue: String): Boolean;
var f: TStringList;
start, i: Integer;
s, OldVal: String;
begin
Result:=FALSE;
f:=TStringList.Create;
try
if not FileExists(FileName) then Exit;
// Grab file into TStringList:
f.LoadFromFile(FileName);
// Find first occurence of line with HeaderString:
start:=f.IndexOf(HeaderStr ing);
if start=-1 then Exit;
// Next line should have ValueString:
s:=f.Strings[start+1];
ValueString:=ValueString+' =';
i:=Pos(ValueString, s);
if i>0 then begin
// Get value string and next 2 chars:
OldVal:=Copy(s, i+Length(ValueString), 2);
// Replace this with NewValue:
f.Strings[start+1]:=String Replace(s, ValueString+OldVal, ValueString+NewValue,
[rfIgnoreCase]);
Result:=TRUE;
f.SaveToFile(FileName);
end;
finally
f.Free;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '00') then
ShowMessage('Replaced OK.')
else
ShowMessage('Error!');
end;
Copy/Paste (place Button1 on the form):
---------------
function ReplaceIt(FileName, HeaderString, ValueString, NewValue: String): Boolean;
var f: TStringList;
start, i: Integer;
s, OldVal: String;
begin
Result:=FALSE;
f:=TStringList.Create;
try
if not FileExists(FileName) then Exit;
// Grab file into TStringList:
f.LoadFromFile(FileName);
// Find first occurence of line with HeaderString:
start:=f.IndexOf(HeaderStr
if start=-1 then Exit;
// Next line should have ValueString:
s:=f.Strings[start+1];
ValueString:=ValueString+'
i:=Pos(ValueString, s);
if i>0 then begin
// Get value string and next 2 chars:
OldVal:=Copy(s, i+Length(ValueString), 2);
// Replace this with NewValue:
f.Strings[start+1]:=String
[rfIgnoreCase]);
Result:=TRUE;
f.SaveToFile(FileName);
end;
finally
f.Free;
end;
end;
procedure TForm1.Button1Click(Sender
begin
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '00') then
ShowMessage('Replaced OK.')
else
ShowMessage('Error!');
end;
OOPS!
Sorry nestorua, didn't read your comment (great minds think the same :=) ...
typfing, if you are satisfied with the code, please
give nestorua half of the points.
Sorry nestorua, didn't read your comment (great minds think the same :=) ...
typfing, if you are satisfied with the code, please
give nestorua half of the points.
OOPS #2!
I'm a bit sleepy today...
typfing, replace:
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '00') then
with:
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '03') then
I'm a bit sleepy today...
typfing, replace:
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '00') then
with:
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '03') then
ASKER
heck, i'm totally CONFUSED....argh
typfing,
> heck, i'm totally CONFUSED....
OK, I will be very detailed, but please don't take this as patronising...
1. Start new application
2. Place Button1 on a Form
3. Goto Unit1.pas source, copy and paste following function:
function ReplaceIt(FileName, HeaderString, ValueString, NewValue: String): Boolean;
var f: TStringList;
start, i: Integer;
s, OldVal: String;
begin
Result:=FALSE;
f:=TStringList.Create;
try
if not FileExists(FileName) then Exit;
// Grab file into TStringList:
f.LoadFromFile(FileName);
// Find first occurence of line with HeaderString:
start:=f.IndexOf(HeaderStr ing);
if start=-1 then Exit;
// Next line should have ValueString:
s:=f.Strings[start+1];
ValueString:=ValueString+' =';
i:=Pos(ValueString, s);
if i>0 then begin
// Get value string and next 2 chars:
OldVal:=Copy(s, i+Length(ValueString), 2);
// Replace this with NewValue:
f.Strings[start+1]:=String Replace(s, ValueString+OldVal, ValueString+NewValue,
[rfIgnoreCase]);
Result:=TRUE;
f.SaveToFile(FileName);
end;
finally
f.Free;
end;
end;
4. Double-click Button1, and Copy/Paste this inside its event handler:
procedure TForm1.Button1Click(Sender : TObject);
begin
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '03') then
ShowMessage('Replaced OK.')
else
ShowMessage('Error!');
end;
-------------------------- ---
Function which does what you want is ReplaceIt(). Its arguments are:
1. FileName - full path of the file you are parsing
(in your case 'C:\data.txt')
2. HeaderString - The line that marks the start of the
block you want to change (in your case
it's 'Peter', but can be changed
to 'John')
3. ValueString - The following line must contain this
string, then '=', then 2 digits (in your
case it's 'Level'
4. NewValue - Digits will be replaced with this string
(you wanted '03')
-------------------------- ----
Is this any clearer?
> heck, i'm totally CONFUSED....
OK, I will be very detailed, but please don't take this as patronising...
1. Start new application
2. Place Button1 on a Form
3. Goto Unit1.pas source, copy and paste following function:
function ReplaceIt(FileName, HeaderString, ValueString, NewValue: String): Boolean;
var f: TStringList;
start, i: Integer;
s, OldVal: String;
begin
Result:=FALSE;
f:=TStringList.Create;
try
if not FileExists(FileName) then Exit;
// Grab file into TStringList:
f.LoadFromFile(FileName);
// Find first occurence of line with HeaderString:
start:=f.IndexOf(HeaderStr
if start=-1 then Exit;
// Next line should have ValueString:
s:=f.Strings[start+1];
ValueString:=ValueString+'
i:=Pos(ValueString, s);
if i>0 then begin
// Get value string and next 2 chars:
OldVal:=Copy(s, i+Length(ValueString), 2);
// Replace this with NewValue:
f.Strings[start+1]:=String
[rfIgnoreCase]);
Result:=TRUE;
f.SaveToFile(FileName);
end;
finally
f.Free;
end;
end;
4. Double-click Button1, and Copy/Paste this inside its event handler:
procedure TForm1.Button1Click(Sender
begin
if ReplaceIt('C:\data.txt', 'Peter', 'Level', '03') then
ShowMessage('Replaced OK.')
else
ShowMessage('Error!');
end;
--------------------------
Function which does what you want is ReplaceIt(). Its arguments are:
1. FileName - full path of the file you are parsing
(in your case 'C:\data.txt')
2. HeaderString - The line that marks the start of the
block you want to change (in your case
it's 'Peter', but can be changed
to 'John')
3. ValueString - The following line must contain this
string, then '=', then 2 digits (in your
case it's 'Level'
4. NewValue - Digits will be replaced with this string
(you wanted '03')
--------------------------
Is this any clearer?
ASKER
yes thanx.how abt i give all the points to u and u share it out ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
HI,
Do as you want.
Sincerely,
Nestorua.
Do as you want.
Sincerely,
Nestorua.
You can do it in different ways:
1. Using TIniFile.
2. Using TStringList, finding the string "Peter",
then the next string is "Level=02", change digit "2" on digit
"3" as you do it working with strings.
Or work with that string as you do with the string
"A=B".
Sincerely,
Nestorua.