Task: In the folder there is a list of files. It is necessary to pull out the lines for the keyword - ' Zadaniye ' from each file in the folder and write these lines into one file ( Save.txt ).

The problem with me, in the following: It turns out that the results are recorded in different files, but you need one.

An example to clarify:

The folder has files 1.txt, 2.txt, 3.txt . The program searches for all the lines where there is the word Zadaniye in these files and:

Writes the result like this:

Save_1.txt Save_2.txt Save_3.txt 

And you need all the results found from all files - write to one file. I.e:

 Save.txt 

Where did I make a mistake? I'm doing this one, but with me:

 // ищем все файлы в папке procedure findfile2(dir, path: string; var file_list: TStringList); var SR: TSearchRec; FindRes: Integer; begin FindRes := FindFirst(dir + path, faAnyFile - faDirectory, SR); while FindRes = 0 do begin file_list.Add(dir + SR.name); FindRes := FindNext(SR); end; FindClose(SR); end; procedure TForm1.Button1Click(Sender: TObject); var fall, f1: TextFile; S: string; i: Integer; file_list: TStringList; flname1, flname2: string; p: string; begin file_list:=TStringList.Create; // расширение файлов p:='*' + ExtractFileExt(flname); // Путь к папке findfile2('C:\Users\Tatyana\Desktop\Новая папка\T'+'\',p,file_list); //Делаем проход по файлам for i:=0 to file_list.Count-1 do begin flname1:=file_list.Strings[i]; flname2:=ExtractFileDir(flname1)+'\'+'Save_'+extractfilename(flname1); // Открываем каждый файл в цикле AssignFile(fall, flname1); reset(f1); // Записываем результат AssignFile(f1, flname2); rewrite(f1); // работаем по строкам в файле while not Eof(fall) do begin // читаем строку ReadLn(fall, S); if (PosEx('Zadaniye', S) > 0) then writeln(f1, S); end; CloseFile(fall); CloseFile(f1); end; end; end. 

    1 answer 1

    Take out

      // Записываем результат AssignFile(f1, 'Save.txt'); rewrite(f1); 

    and

      CloseFile(f1); 

    for the cycle.

    • 3
      The file has been opened - the cycle has begun - the lines are being written, we are writing, we are writing - the cycle has ended - the file has been closed - MBo
    • All figured out and made according to your recommendations. Everything works ... - Tatiana