I want to make a new .txt file which parameters i load from a .script file. It works fine in the root. .script file is in the root directry.The result is:
- 192.121.2.2 login Lucey password red
- 192.321.2.1 login Steve password apple
.
if FindFirst('*.*', faAnyFile, Search) = 0 then
begin
while FindNext(Search) = 0 do
begin
if FileExists(Search.Name) then
begin
if pos('.txt', Search.Name) <> 0 then
begin
Assignfile(txtfile, Search.Name);
Erase(txtfile);
end;
end;
end;
end;
findclose(Search);
if FindFirst('*.*', faAnyFile, Search) = 0 then
begin
while FindNext(Search) = 0 do
begin
if FileExists(Search) then
begin
if pos('.script', Search.Name) <> 0 then
begin
data_file := TStringList.Create;
data_file.LoadFromFile(Search.Name);
txtdata := data_file.Strings[1].+ 'login' + data_file.Strings[2] +
'password' + data_file.Strings[3];
txtfile := TStringList.Create;
if FileExists('.txt') then
begin
txtfile.LoadFromFile('.txt');
end;
txtfile.Add(txtdata);
txtfile.SaveToFile('.txt');
end;
end;
end;
end;
findclose(Search);
But if i want to add C:\Script\Files the code i use is wrong, what is the problem? Pls help!
if FindFirst('c:\Script\Files\*.*', faAnyFile, Search) = 0 then
begin
while FindNext(Search) = 0 do
begin
if FileExists(Search.Name) then
begin
if pos('c:\Script\Files\.txt', Search.Name) <> 0 then
begin
Assignfile(txtfile, Search.Name);
Erase(txtfile);
end;
end;
end;
end;
findclose(Search);
if FindFirst('*.*', faAnyFile, Search) = 0 then
begin
while FindNext(Search) = 0 do
begin
if FileExists(Search) then
begin
if pos('.script', Search.Name) <> 0 then
begin
data_file := TStringList.Create;
data_file.LoadFromFile(Search.Name);
txtdata := data_file.Strings[1] + 'login' + data_file.Strings[2] +
'password' + data_file.Strings[3];
txtfile := TStringList.Create;
if FileExists('c:\Script\Files\.txt') then
begin
txtfile.LoadFromFile('c:\Script\Files\.txt');
end;
txtfile.Add(txtdata);
txtfile.SaveToFile('c:\Script\Files\.txt');
end;
end;
end;
end;
findclose(Search);
The result is:
- 192.121.2.2 login Lucey password red
- 192.121.2.2 login Lucey password red
- 192.321.2.1 login Steve password apple
The 1st item shows twice etc.
You have quite a lot of logic bugs in your code. Try this instead: