我想创建一个新的.txt文件,我从.script文件加载参数。它在root中运行良好。 .script文件位于root directry中。结果是:
- 192.121.2.2登录Lucey密码红色
- 192.321.2.1登录史蒂夫密码苹果
醇>
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);
但如果我想添加C:\ Script \ Files我使用的代码是错误的,有什么问题?请帮助!
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);
结果是:
- 192.121.2.2登录Lucey密码红色
- 192.121.2.2登录Lucey密码红色
- 192.321.2.1登录史蒂夫密码苹果
醇>
第一项显示两次等。
答案 0 :(得分:2)
你的代码中有很多逻辑错误。试试这个:
if FindFirst('c:\Script\Files\*.txt',faAnyFile,Search) = 0 then
begin
repeat
DeleteFile('c:\Script\Files\' + Search.Name);
until FindNext(Search) <> 0;
FindClose(Search);
end;
txtfile := TStringList.Create;
try
if FindFirst(PathToScriptFiles + '*.script',faAnyFile,Search) = 0 then
begin
repeat
data_file := TStringList.Create;
try
data_file.LoadFromFile(PathToScriptFiles + Search.Name);
txtdata := data_file.Strings[1] + ' login ' + data_file.Strings[2] + ' password ' + data_file.Strings[3];
txtfile.Add(txtdata);
finally
data_file.Free;
end;
until FindNext(Search) <> 0;
FindClose(Search);
end;
txtfile.SaveToFile('c:\Script\Files\.txt');
finally
txtfile.Free;
end;