创建多个文本文件

发布于 2024-11-26 21:48:09 字数 427 浏览 3 评论 0原文

在应用程序上创建多个 *.txt 文件的巧妙方法是什么 启动即检查它们是否存在,如果不创建它们。 我需要创建大约 10 个文本文件。 我是否必须对每个文件都这样做:

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尾戒 2024-12-03 21:48:09

如果您只想使用随后编号的文件名创建空文件(或重写现有文件),您可以尝试类似的操作。以下示例使用 CreateFile API功能。但请注意,有几件事可能会禁止您尝试创建文件!

如果您想在所有情况下创建(覆盖)它们,请使用 CREATE_ALWAYS 处置标志

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Name: string;
  Path: string;
begin
  Path := ExtractFilePath(ParamStr(0));
  for I := 1 to 10 do
    begin
      Name := Path + 'a' + IntToStr(I) + '.txt';
      CloseHandle(CreateFile(PChar(Name), 0, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
    end;
end;

或者如果您只想在文件不存在时创建它们,请使用 CREATE_NEW 处置标志

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Name: string;
  Path: string;
begin
  Path := ExtractFilePath(ParamStr(0));
  for I := 1 to 10 do
    begin
      Name := Path + 'a' + IntToStr(I) + '.txt';
      CloseHandle(CreateFile(PChar(Name), 0, 0, nil, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0));
    end;
end;

If you only want to create the empty files (or rewrite the existing) with subsequently numbered file names, you might try something like this. The following examples use the CreateFile API function. But pay attention that several things may forbid your file creation attempts!

If you want to create (overwrite) them in all circumstances, use CREATE_ALWAYS disposition flag

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Name: string;
  Path: string;
begin
  Path := ExtractFilePath(ParamStr(0));
  for I := 1 to 10 do
    begin
      Name := Path + 'a' + IntToStr(I) + '.txt';
      CloseHandle(CreateFile(PChar(Name), 0, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
    end;
end;

Or if you want to create the files only if they doesn't exist, use the CREATE_NEW disposition flag

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Name: string;
  Path: string;
begin
  Path := ExtractFilePath(ParamStr(0));
  for I := 1 to 10 do
    begin
      Name := Path + 'a' + IntToStr(I) + '.txt';
      CloseHandle(CreateFile(PChar(Name), 0, 0, nil, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0));
    end;
end;
月亮坠入山谷 2024-12-03 21:48:09

也许是这样的:

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;

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;
长发绾君心 2024-12-03 21:48:09
  procedure CreateFile(Directory: string; FileName: string; Text: string);
  var
    F: TextFile;
  begin
    try
      AssignFile(F, Directory + '\' + FileName);
      {$i-}
      Rewrite(F);
      {$i+}
      if IOResult = 0 then
      begin
         Writeln(F, Text);
      end;
    finally
      CloseFile(f);
    end;
  end;
  ...

  for i := 0 to 10 do
    CreateFile(Directory, Filename, Text);
  procedure CreateFile(Directory: string; FileName: string; Text: string);
  var
    F: TextFile;
  begin
    try
      AssignFile(F, Directory + '\' + FileName);
      {$i-}
      Rewrite(F);
      {$i+}
      if IOResult = 0 then
      begin
         Writeln(F, Text);
      end;
    finally
      CloseFile(f);
    end;
  end;
  ...

  for i := 0 to 10 do
    CreateFile(Directory, Filename, Text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文