4

I'm french so sorry for my little language...

So, my project is here Memo and create file and folder?

i have a problem with my code :

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;

my structure in Tmemo is :

structure of tmemo

and my bad result:

bad result

i test first and last character for know what is it file or folder and my problem is file saved in currentPath, no in folder1:

Dir:

 folder1->file1.txt 
  folder2 ->file2.txt and file2-3.txt
   etc..

can you help me please?

Thanks a lot.

Community
  • 1
  • 1
Kate
  • 320
  • 1
  • 6
  • 19

2 Answers2

2

You have to test first.
is the token a directory then isFolder:=true.

"memo2" is just about to create file.txt easier. (more convenient)

Delphi 5

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.

UPDATE : \file1.txt:Hello to the world

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;

Remember to delete file1.txt if it is present
Do'nt forget to set Memo2.Text:='' if there is not : Otherwise, all files of the same text !!
Try it with if count > 0 then begin [...] else Memo2.Text:=''

moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • Your script is work,good idea for using Tmemo2 so very thank you for your example... – Kate Oct 28 '12 at 13:25
  • 1
    Glad I could help. It is not necessary to call 'ExtractFilePath (Edit1.Text)'. Because if the path ends without "\", then `C:\mydir\mydir2` becomes to `C:\mydir\`. And that is not what you wanted to. – moskito-x Oct 28 '12 at 16:28
  • Hi moskito-x, Simple question, it's possible with your code i add text in file with delimiter example: file1.txt *:* write_is_work ? – Kate Oct 30 '12 at 01:09
  • @Kate Can you explain in more detail what you want to do? – moskito-x Nov 02 '12 at 19:52
  • Ok if i use delimiter **:** in memo example test.txt:hello = write hello in test.txt – Kate Nov 02 '12 at 23:40
  • @Kate Look at my updated answer. test.txt do'nt start with `\\` . – moskito-x Nov 03 '12 at 00:14
  • @ moskito-x Than you for reply, yes for test it's perfect now, my solution for write text in file http://pastebin.com/81jkjXkA you have any suggestions? – Kate Nov 03 '12 at 02:13
  • You did not use Memo2.Text to write the file ?? Wy not ? Also do NOT use components (Edit3) `Edit3.Text := Path;` for that !! And if there is no `:` in your Token the file will not be written !!. Your logic does not apply then. – moskito-x Nov 03 '12 at 02:38
  • @Kate: If you have a new, separate question, ask it as one, and post your new code **here** in your new question instead of on pastebin. This question was answered and the answer accepted. Please don't change it now. Thanks. – Ken White Nov 03 '12 at 03:44
1
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;

This assumes that folder# will always be the folder string with # = number. Similar with files.

Merlin W.
  • 231
  • 2
  • 11
  • **num := StrToInt(Memo1.Lines[i][7]);** This assumes that all files are only 7 characters long and in the end have a number? In real life that is hardly the case! (preprogrammed errors). – moskito-x Oct 28 '12 at 04:54
  • @moskito-x it seemed from the OP code that files and folders have the same structure based on `folderCharacter` characters and its position in the string. But that is why I made the comment regarding the structure and used same OP code for the most part. – Merlin W. Oct 28 '12 at 04:57
  • @moskito-x it works for that too :<) this code I tried before I posted. `num := StrToInt(Memo1.Lines[i][7]` only gets the number from the 7th position while that string is actually 9 characters long based on the OP images. I compare only the folder number with the files first number, so `file2-3.txt` would end up in `folder2` – Merlin W. Oct 28 '12 at 05:06
  • Thank you for helped me, The problem is that the values ​​can be random and with your code it's no functional... – Kate Oct 28 '12 at 13:38