https://drive.google.com/file/d/1-ajoqVmw2-WeLMG5f2CR33__fjK6WZY8/view?usp=sharing{
Gary Russell (ISSW)
ghrussell@yahoo.com
Create a console window for input.
If you close the console window.
The GUI will close too.
Except on Program Console button.
Show 3 way to use the console.
Buttons:
Open Console button.
Open console for enput.
Press the Q hey end input.
Memo console button.
Open console for input.
Press the Q hey end input.
But send the output to Memo1.
Program Console button.
Open a console.
Uses RunCommand to do a dir command.
dir /s is all files in the directory and it's sub-directorys.
And put the Console output into a string s.
Close the console.
Copy the s string into the memo box.
Note:
If you use Program Console on large directory/sub-directory.
It will hold the console window open for a long time.
}
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Windows, Process;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo1: TMemo;
SelectDirectoryDialog1: TSelectDirectoryDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
public
end;
const
BUF_SIZE = 2048; // Buffer size for reading the output in chunks
var
Form1: TForm1;
FileN: String;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
///AllocConsole initializes standard input, standard output,
// and standard error handles for the new console.
AllocConsole; //in Windows unit. Win32 API.
///True for console applications, False for GUI applications.
IsConsole := True; //in System unit.
///Initialization of standard file variables
// (Input, Output, ErrOutput, StdOut and StdErr).
SysInitStdIO; //in System unit.
///Write to the console window.
writeln('Enter something or Q to Quit');
///Repeat ontell you type in a capital Q.
repeat begin
///Read Line. Read a line of text from the keyboard.
// (Enter ends the line) And put the line into a string s.
readln(s);
///After you press the Enter key.
// Print 'You typed: ' and the string s.
// To the console.
writeln('You typed: ' + s);
///When you enter Q and press the Enter key. Loop ends.
end until uppercase(s)='Q';
end;
///Memo console button.
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
///A string List.
StrList: TStringList;
begin
AllocConsole; // in Windows unit
IsConsole := True; // in System unit
SysInitStdIO; // in System unit
writeln('Enter something or Q to Quit');
///Allocate memory for the String List.
StrList := TStringList.Create;
repeat begin
readln(s);
///Add the String s to the String List.
StrList.Add(s);
writeln('You typed: ' + s);
end until uppercase(s)='Q';
///Output the StringList to Memo1.
Memo1.Lines.Text := StrList.Text;
///Deallocate memory used by the StringList.
StrList.Free;
end;
///Program Console button.
procedure TForm1.Button3Click(Sender: TObject);
var
///Create dynamic string.
s: string;
begin
///Salect a directory and put it into FileN
if SelectDirectoryDialog1.Execute then
FileN := SelectDirectoryDialog1.FileName;
///Execute a command in the current working directory.
// And put the output in string s.
// dir /s is all files in the directory and it's sub-directorys.
RunCommand('c:\windows\system32\cmd.exe', ['/c', 'dir /s ' + FileN], s);
///Put the string s into memo1.
// Memo1.Lines is a AnsiString.
// Memo1.Lines.Text is a string.
Memo1.Lines.Text := s;
end;
///Exit
procedure TForm1.Button4Click(Sender: TObject);
begin
Form1.Close;
end;
end.