1

Как обрабатывать несколько форматов сразу - в данном коде (К примеру .txt и .php):

procedure ScanDir(path: string);
var
  sr: TSearchRec;
  FileAttrs: Integer;
  s: tstringlist;

begin
  path := IncludeTrailingPathDelimiter(path);
  if FindFirst(path + '*.txt*', faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Name <> '.') and (sr.Name <> '..') then
        if (sr.Attr and faDirectory) = faDirectory then
        begin
          Form1.Memo1.Lines.Add('Путь к обработанному файлу: ' + path +
            sr.Name);
          ScanDir(path + sr.Name);
        end
        else
        begin
          s := tstringlist.create;
          s.LoadFromFile(path + sr.Name);
          s.text := StringReplace(s.text, Form1.Memo2.Lines[0],
            Form1.Memo3.Lines[0], [rfreplaceall]);
          s.text := StringReplace(s.text, Form1.Memo2.Lines[1],
            Form1.Memo3.Lines[1], [rfreplaceall]);
          s.SaveToFile(path + Form1.Edit2.text + sr.Name); 
          FreeAndNil(s);
        end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ScanDir(Edit1.text + '\');
end;

Я делала вот так: повторяла цикл if FindFirst(path + '.txt', faAnyFile, sr) - несколько раз, но это не профессионально.

//////////////////////////////
 if FindFirst(path + '*.txt*', faAnyFile, sr) = 0 then
      begin
        repeat
          if (sr.Name <> '.') and (sr.Name <> '..') then
            if (sr.Attr and faDirectory) = faDirectory then
            begin
              Form1.Memo1.Lines.Add('Путь к обработанному файлу: ' + path +
                sr.Name);
              ScanDir(path + sr.Name);
            end
            else
            begin
              s := tstringlist.create;
              s.LoadFromFile(path + sr.Name);
              s.text := StringReplace(s.text, Form1.Memo2.Lines[0],
                Form1.Memo3.Lines[0], [rfreplaceall]);
              s.text := StringReplace(s.text, Form1.Memo2.Lines[1],
                Form1.Memo3.Lines[1], [rfreplaceall]);
              s.SaveToFile(path + Form1.Edit2.text + sr.Name); 
              FreeAndNil(s);
            end;
        until FindNext(sr) <> 0;
        FindClose(sr);
      end;

/////////////////////////////
if FindFirst(path + '*.php*', faAnyFile, sr) = 0 then
      begin
        repeat
          if (sr.Name <> '.') and (sr.Name <> '..') then
            if (sr.Attr and faDirectory) = faDirectory then
            begin
              Form1.Memo1.Lines.Add('Путь к обработанному файлу: ' + path +
                sr.Name);
              ScanDir(path + sr.Name);
            end
            else
            begin
              s := tstringlist.create;
              s.LoadFromFile(path + sr.Name);
              s.text := StringReplace(s.text, Form1.Memo2.Lines[0],
                Form1.Memo3.Lines[0], [rfreplaceall]);
              s.text := StringReplace(s.text, Form1.Memo2.Lines[1],
                Form1.Memo3.Lines[1], [rfreplaceall]);
              s.SaveToFile(path + Form1.Edit2.text + sr.Name);
              FreeAndNil(s);
            end;
        until FindNext(sr) <> 0;
        FindClose(sr);
      end;

1 ответ 1

1

Татьяна, Вы совершенно правы. Копирование кода - не наш метод.

procedure ScanDirWithMask(path: string; mask: string);
var
  sr: TSearchRec;
  FileAttrs: Integer;
  s: tstringlist;
begin
  path := IncludeTrailingPathDelimiter(path);
  if FindFirst(path + mask, faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Name <> '.') and (sr.Name <> '..') then
        if (sr.Attr and faDirectory) = faDirectory then
        begin
          Form1.Memo1.Lines.Add('Путь к обработанному файлу: ' + path + sr.Name);
          ScanDir(path + sr.Name);
        end
        else
        begin
          s := tstringlist.create;
          s.LoadFromFile(path + sr.Name);
          s.text := StringReplace(s.text, Form1.Memo2.Lines[0], Form1.Memo3.Lines[0], [rfreplaceall]);
          s.text := StringReplace(s.text, Form1.Memo2.Lines[1], Form1.Memo3.Lines[1], [rfreplaceall]);
          s.SaveToFile(path + Form1.Edit2.text + sr.Name); 
          FreeAndNil(s);
        end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

procedure ScanDir(path: string);
begin
  ScanDirWithMask(path, '*.txt*');
  ScanDirWithMask(path, '*.php*');
end;
8
  • Вот такое сообщение: undeclared identifier ScanDirWithMask. Что присвоить в var ScanDirWithMask ? 5 июн 2018 в 21:12
  • @Татьяна просто поменяйте процедуры местами
    – user176262
    5 июн 2018 в 21:13
  • Поменяла и теперь пишет сообщение: undeclared identifier ScanDir. 5 июн 2018 в 21:15
  • @Татьяна на какой строчке ошибка?
    – user176262
    5 июн 2018 в 21:16
  • @Татьяна ScanDir(Edit1.text + '\'); - на этой?
    – user176262
    5 июн 2018 в 21:18

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.