filedelphiapplication-start

Create multiple text files


what would be a neat way to create multiple *.txt files on application startup i.e check if they exist if not create them. I need to create about 10 text files. Will I have to do like this for every single file:

var
  MyFile: textfile;
  ApplicationPath: string;
begin
  ApplicationPath := ExtractFileDir(Application.ExeName);
  if not FileExists(ApplicationPath + '\a1.txt') then
    begin
      AssignFile(MyFile, (ApplicationPath + '\a1.txt'));
      Rewrite(MyFile);
      Close(MyFile);
    end
  else 
    Abort;
end;

Solution

  • Something like this, perhaps:

    var
        ApplicationDir: string;
        I: Integer;
        F: TextFile;
    begin
        ApplicationDir := ExtractFileDir(Application.ExeName);
        for I := 1 to 10 do
          begin
            Path := ApplicationDir + '\a' + IntToStr(I) + '.txt';
            if not FileExists(Path) then
              begin
                AssignFile(F, Path);
                Rewrite(F);
                Close(F);
              end
          end;