欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

delphi创建文件夹和文件

程序员文章站 2022-06-14 15:24:09
...

delphi创建文件夹和文件

使用ForceDirectories函数,该函数支持多级文件夹创建

1.创建文件夹

//全局变量
const
rmb_suffixs : array[0..6] of string = ('.txt','.txt','0.txt','00.txt','000.txt','0000.txt','00000.txt');

var
paths : array[0..5] of string;
files : array[0..4] of TextFile;
//origin:磁盘根目录 例如D:\xxx
procedure makeDir(origin:string;data:ISuperObject);
begin
  if origin <> '' then
  begin
     paths[0] := origin + '\Mir200\Envir';
     paths[1] := origin + '\Mir200\Envir\Market_Def';
     paths[2] := origin + '\Mir200\Envir\QuestDiary\充值'+'元宝'+'\'+'元宝';
     paths[3] := origin + '\Mir200\Envir\QuestDiary\充值'+'元宝'+'\附加赠送';
     paths[4] := origin + '\Mir200\Envir\QuestDiary\充值'+'元宝'+'\积分充值';
     paths[5] := origin + '\Mir200\Envir\QuestDiary\充值'+'元宝'+'\装备赠送';
     
     paths[3] := paths[3]+'\'+'金刚石'+'赠送';
     paths[4] :=  paths[4]+'\'+'消费积分'+'充值';
     if not System.SysUtils.DirectoryExists(paths[0]) then
        System.SysUtils.ForceDirectories(paths[0]);
     if not System.SysUtils.DirectoryExists(paths[1]) then
        System.SysUtils.ForceDirectories(paths[1]);
     if not System.SysUtils.DirectoryExists(paths[2]) then
        System.SysUtils.ForceDirectories(paths[2]);
     if not System.SysUtils.DirectoryExists(paths[3]) then
        System.SysUtils.ForceDirectories(paths[3]);
     if not System.SysUtils.DirectoryExists(paths[4]) then
        System.SysUtils.ForceDirectories(paths[4]);
     if not System.SysUtils.DirectoryExists(paths[5]) then
        System.SysUtils.ForceDirectories(paths[5]);
  end;
end;

2.创建txt文件

procedure makeNpcFile(data:ISuperObject);
var
list : TStringList;
begin
  try
    if System.SysUtils.DirectoryExists(paths[1]) then
    begin
      list := TStringList.Create;
      list := npc(data);
      AssignFile(files[0],paths[1]+'\'+'元宝充值使者-1'+rmb_suffixs[0]);
      Rewrite(files[0]);
      Append(files[0]);
      Writeln(files[0],list.Text.Trim);
      CloseFile(files[0]);
      list.Free;
    end;
  except
    utils.log('创建npc使者失败');
  end;