You must test first.
is the token a directory , then isFolder:=true .
"memo2" is just about to create a .txt file. (More comfortable)
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 present
Remember to set Memo2.Text:='' if not : Otherwise, all files are of the same text!
Try using if count > 0 then begin [...] else Memo2.Text:=''