1

I'm new to Delphi and i'm a french user so sorry for my bad english...

So it is possible to create a file written in TMemo?

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

My TMemo there are 3 lines, so I would like to take the first line and create the file test.txt 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 Directory and files? etc...

So my conclusion was to automate it.

You can help me please?

a small image so you can see:

Automate create files&folder

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
Kate
  • 320
  • 1
  • 6
  • 19

2 Answers2

3

With Program and on ButtonClick event If I have understood the question correctly, this will

  1. Create an empty text file in the application directory with the filename as per Memo Line 1
  2. Create Folders as per Memo Line 2 and in the "base directory" per the edit
  3. Create a Folder and empty TextFile as per Memo 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 needs significantly more error checking determining if paths valid and files / directories created etc...

Paul Heinrich
  • 647
  • 1
  • 7
  • 15
  • 1
    Use of pascal file access methods is advised against since D2009 because they are not Unicode compatible. Use streams instead. – Marjan Venema Oct 23 '12 at 07:36
0

Edit: code in browser so have no idea if it works but really is a simple thing to do.

You should only use a TMemo if you want to display the data before you save it, because the task of a visual control is to display something. But if you only want to use the Items propery of the TMemo for collecting strings and then save them to 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 thread: http://www.tek-tips.com/viewthread.cfm?qid=678231

EDIT2:

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

Provided that Edit Control is named Edit1 and has your base path and first line of the TMemo has the file name. The other bit you need is an Event and by that I mean if you doubleclick your TMemo instance it will be the event that will start the cascade for saving the file.

As you see it is very easy and there are other ways such as SaveDialog that can make it much easier still. but hope this answers your question.

Merlin W.
  • 231
  • 2
  • 11
  • The whole iteration you can replace with `sl.Assign(Memo1.Lines);`, but still I don't get your idea somehow. You're just saving all the lines from a memo box to the file specified in its first line. – TLama Oct 23 '12 at 02:21
  • This doesn't answer the question asked (and as @TLama says, it can all be replaced with six lines of code without the looping and one less variable (the loop counter), and you're saving to whatever happens to be in the second line of the memo (`Memo1.Lines[1]`)). Actually, it can be replaced with a single line of code with *no* variables: `Memo1.Lines.SaveToFile(Memo1.Lines[0]);`. – Ken White Oct 23 '12 at 02:22
  • Sure you can use `assign` as well. No one way of doing things is there? The idea about saving directly from a TMemo without using an intermediate object I simply never tried but still `AssignFile` or even `Memo1.Lines.SaveToFile("line number that has full path")` – Merlin W. Oct 23 '12 at 02:29
  • @KenWhite I didn't see that part in your comment `Memo1.Lines.SaveToFile` untill I finished posting mine :) well that is another way. – Merlin W. Oct 23 '12 at 02:32
  • @TLama I'm just trying to provide an idea on how it's done but I'm not sure what is the OP doing with the lines from TMemo. Unusual way if I may say so but certainly can be done easy. – Merlin W. Oct 23 '12 at 02:34
  • 2
    Bad habit here at SO of downvoting without commenting. I am getting used to it though as this happens too frequently. But hey it's your world and I'm just in it. – Merlin W. Oct 23 '12 at 02:57
  • I thought I made it quite clear why I downvoted. Perhaps I should have stated clearly "I'm downvoting this because..." - I usually start with `-1.`, but must have accidentally omitted it. With that being said, the reason people don't comment about why they downvote often is probably because the downvoted poster then starts revenge downvoting. Also, there's no requirement to post a reason for downvoting; if there was, an explanation would be needed before the downvote was allowed. An explanation is a courtesy that not everyone extends. (I do, thus my comment above.) – Ken White Oct 25 '12 at 01:15
  • @KenWhite well thanks. You are the first to comment on why downvote. I don't agree with your downvote but I'm not one that downvote easily. Actually I have yet to downvote anyone answer or question :) I usually upvote questions that I feel wrongly downvoted because the question doesn't showcase a newer delphi version or actually manages to ask a coding question beyond already made components or solutions which again just showcase delphi. Anyhow everyone is entitled to their own opinion. – Merlin W. Oct 25 '12 at 01:26
  • 2
    I don't downvote frequently either (check my profile). I do when the answer is clearly wrong. :-) Your answer did not address the question asked at all, and the entire code you posted could have been replaced with the one line I posted in my previous comment as early as Delphi 1 (15 years ago), so it has nothing to do with "showcasing a newer Delphi version". It was wrong from the first version of Delphi - sorry. :-) – Ken White Oct 25 '12 at 01:33