Remind and create file and folder?

I am new to Delphi and I am a French user, so sorry for my poor English ...

So, can you create a file written in TMemo ?

test.txt dir1/dir2/text.txt dir3/ 

There are 3 lines in my TMemo, so I would like to take the first line and create a test.txt file in the current directory.

 2nd line: create a folder 3rd line: create a folder again+files.txt etc ... 

I think to use mkdir or ForceDirectories to create a directory and files? etc...

So my conclusion was to automate it.

Could you help me?

small image so you can see:

Automate create files & folder

+1
source share
2 answers

With ButtonClick program and event. If I understand the question correctly, it will be

  • Create an empty text file in the application directory with the file name according to the note line 1
  • Create folders according to note line 2 and in the "base directory" for editing
  • Create a folder and an empty TextFile according to note line 3 again in the "base directory"

BasicAppExample

 procedure TForm1.Button1Click(Sender: TObject); var Path: String; F: TextFile; begin // Create File in current directory Path := ExtractFilePath(ParamStr(0)) + Memo1.Lines.Strings[0]; if not FileExists(Path) then begin AssignFile(F, Path); Rewrite(F); //Writeln(F, 'text to write to file'); CloseFile(F); end; // Create Directories Path := IncludeTrailingPathDelimiter(edPath.Text) + Memo1.Lines.Strings[1]; if not DirectoryExists(Path) then ForceDirectories(Path); // Create Directory and File Path := IncludeTrailingPathDelimiter(edPath.Text) + Memo1.Lines.Strings[2]; if not DirectoryExists(ExtractFilePath(Path)) then ForceDirectories(ExtractFilePath(Path)); if not FileExists(Path) then begin AssignFile(F, Path); Rewrite(F); //Writeln(F, 'text to write to file'); CloseFile(F); end; end; 

Obviously, a significantly larger error check is required to determine if paths are valid and files / directories are created, etc.

+3
source

Edit: The code is in the browser, so I have no idea if it works, but it's really simple.

You should use TMemo only if you want to display data before saving it, because the task of visual control is to display something. But if you want to use the Items TMemo property to collect strings and then save them to a file, you should use a TStringList instead:

 var i: Integer; sl: TStringList; begin sl := TStringList.Create; try for i := 0 to Memo1.Lines.Count-1 do sl.Add(Memo1.Lines[i]); sl.SaveToFile(sl[1]); finally sl.free; end; end; 

You may also like this theme: http://www.tek-tips.com/viewthread.cfm?qid=678231

EDIT2:

 Memo1.Lines.SaveToFile(edit1.text + Memo1.Lines[0]); 

Provided that Edit Control is called Edit1, both your base path and the first line of TMemo have a file name. The other bit you need is Event , and I mean, if you double-click an instance of TMemo, it will be an event that will trigger a cascade to save the file.

As you can see, this is very simple, and there are other ways like SaveDialog that can make it a lot easier. but hope this answers your question.

0
source

Source: https://habr.com/ru/post/1442565/


All Articles