-1

我想制作一个新的 .txt 文件,我从 .script 文件加载参数。它在根中工作正常。.script 文件位于根目录中。结果是:

  1. 192.121.2.2登录Lucey密码红色
  2. 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);

结果是:

  1. 192.121.2.2登录Lucey密码红色
  2. 192.121.2.2登录Lucey密码红色
  3. 192.321.2.1登录史蒂夫密码苹果

第一项显示两次,依此类推。

4

1 回答 1

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;
于 2012-11-12T19:29:38.590 回答