首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Memo delphi保存file>文件夹

使用Memo delphi保存file>文件夹
EN

Stack Overflow用户
提问于 2012-10-28 09:32:54
回答 2查看 3.4K关注 0票数 4

我是法国人,很抱歉我的语言...

所以,我的项目在这里,Memo and create file and folder?

我的代码有一个问题:

代码语言:javascript
复制
var
  path: String;
  F: TextFile;
  i, e: Integer;
begin
  for i := 0 to Memo1.Lines.Count - 1 do
  begin
    if Length(Memo1.Lines[i]) > 0 then
    begin
      if Memo1.Lines[i][1] = '\' then // first character for file
        if Pos('.', Memo1.Lines[i]) > 0 then // confirm file
        begin
          path := extractfilepath(Edit1.Text) + Memo1.Lines[i];
          // showmessage(path);
          if not FileExists(path) then
          begin
            AssignFile(F, path);
            Rewrite(F);
            CloseFile(F);
          end;
        end;
      e := Length(Memo1.Lines[i]);
      case Memo1.Lines[i][e] of // last character for folder
        '\':
          begin
            path := extractfilepath(Edit1.Text) + Memo1.Lines[i];
            if not DirectoryExists(path) then
              ForceDirectories(path); // create folder
          end;
      end;
    end;
  end;
end;

我在Tmemo中的结构是:

我的坏结果是:

我测试了第一个和最后一个字符,以了解它是什么文件或文件夹,我的问题是文件保存在currentPath中,不保存在folder1中:

目录:

代码语言:javascript
复制
 folder1->file1.txt 
  folder2 ->file2.txt and file2-3.txt
   etc..

你能帮帮我吗?

非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-28 11:50:29

你必须先测试一下。

is the token a directory,然后是isFolder:=true

"memo2“即将更轻松地创建file.txt。(更方便)

Delphi 5

代码语言:javascript
复制
implementation

{$R *.DFM}

uses FileCtrl;

procedure TForm1.FormActivate(Sender: TObject);
begin
Memo1.Text:='test.txt'#13#10'folder1\'#13#10'\file1.txt'#13#10'folder2\'#13#10'\file2.txt'#13#10'\file2-3.txt'#13#10;
Memo2.Text:='';
Edit1.Text:='F:\testdir';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  path,aktpath,actToken: String;
  backSl : Char;
  i: Integer;
  isFolder:Boolean;
begin
backSl := #92; // This only for better reading the code in SO
isFolder:=false;
aktpath:='';actToken:='';
  for i := 0 to Memo1.Lines.Count - 1 do
  begin
    if Length(Memo1.Lines[i]) > 0 then
    begin
      actToken:=Memo1.Lines[i];
      // Folder -----------------------------------------
      if copy(actToken,length(actToken),1)= backSl then begin
      if copy(Edit1.Text,length(Edit1.Text),1)= backSl then
                  path := Edit1.Text + actToken else
                  path := Edit1.Text + backSl + actToken;
         if not DirectoryExists(path) then
              ForceDirectories(path); // create folder
      isFolder:=true;
      aktpath:=path;
      continue;
      end;
      // File   -----------------------------------------
      if copy(actToken,1,1) = backSl then // first character for file
        if Pos('.', actToken) > 0 then // confirm file
        begin
          if isFolder then path:=aktpath + actToken else
                           path:=Edit1.Text + actToken;
          path:=StringReplace(path,'\\',backSl,[rfReplaceAll]);
          if not FileExists(path) then Memo2.Lines.SaveToFile(path);
          continue;
        end;
    end;
  end;
end;

end.

更新:\file1.txt:向世界问好

代码语言:javascript
复制
var
[...]
actTokenTxt: String;
count: Integer;

begin
isFolder:=false;
[...]


// File   -----------------------------------------
      if copy(actToken,1,1) = backSl then // first character for file
        if Pos('.', actToken) > 0 then // confirm file
        begin
          count:=Pos(':', actToken);
          if count > 0 then begin
              actTokenTxt:=copy(actToken,1,count);
              Memo2.Text:=StringReplace(actToken,actTokenTxt,'',[]);
              actToken:=copy(actToken,1,count-1);;
          end;
          if isFolder then path:=aktpath + actToken else
                           path:=Edit1.Text + actToken;
          path:=StringReplace(path,'\\',backSl,[rfReplaceAll]);
          if not FileExists(path) then Memo2.Lines.SaveToFile(path);
          continue;
        end;

如果file1.txt存在,记得将其删除

如果没有:,不要忘记设置Memo2.Text:='',否则,所有相同文本的文件!!

if count > 0 then begin [...] else Memo2.Text:=''试试吧

票数 2
EN

Stack Overflow用户

发布于 2012-10-28 11:51:13

代码语言:javascript
复制
var
  folder_path, path: String;
  F: TextFile;
  i, e, num: Integer;

begin
  for i := 0 to Memo1.Lines.Count - 1 do
  begin
    if Length(Memo1.Lines[i]) > 0 then
    begin
      e := Length(Memo1.Lines[i]);
      if Memo1.Lines[i][e] = '\' then // last character for folder
      begin
        num := StrToInt(Memo1.Lines[i][7]);
        folder_path := Copy(Memo1.Lines[i], 1, Length(Memo1.Lines[i])-1);
        path := extractfilepath(Edit1.Text) + Memo1.Lines[i];
        //showmessage(path);
        if not DirectoryExists(folder_path) then
          ForceDirectories(folder_path); // create folder
      end

      else if Memo1.Lines[i][1] = '\' then // first character for file
        if Pos('.', Memo1.Lines[i]) > 0 then // confirm file
        begin
          if (num = StrToInt(Memo1.Lines[i][6])) then
            path := extractfilepath(Edit1.Text) + folder_path + Memo1.Lines[i]
          else path := extractfilepath(Edit1.Text) + Memo1.Lines[i];
          //showmessage(path);
          if not FileExists(path) then
          begin
            AssignFile(F, path);
            Rewrite(F);
            CloseFile(F);
          end;
        end;
    end;
  end;
end;

这假设folder#始终是带有#= number的文件夹字符串。与文件类似。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13105660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档