[Delphi] How to execute file or dos command like in TotalCmd

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: Hacker, petermad, Stefan2, white

Post Reply
olesio
Junior Member
Junior Member
Posts: 54
Joined: 2009-01-22, 15:29 UTC
Location: Poland

[Delphi] How to execute file or dos command like in TotalCmd

Post by *olesio »

Hello. I want to my Delphi application work like command bar in TotalCmd, so
if I execute application it will be opened, but if I execute dos command like for
example: echo something >> file.ext it will be executed by %ComSpec% from
Env. variable. Now code work but if file do not exist a empty cmd.exe window
is showed. I want to only show messagebox "File not found!". Please helpe me
and show me how to modifity code bellow in Delphi 7 Personal. Thanks in advice.

Code: Select all

function RunAProgram(const TheProgram, ItsParameters, DefaultDirectory: string) : Cardinal;
begin
  Result := ShellExecute(Application.Handle, 'open', PChar (TheProgram),
    PChar (ItsParameters), PChar(DefaultDirectory), SW_SHOWNORMAL);
end;

procedure RunCommand(Cmd, DefDir : string);
var
  X : integer;
  CmdLine : string;
  CmdBuffer: array[0..MAX_PATH] of Char;
begin
  CmdLine := '';
  Cmd := TrimLeft(Cmd);
  X := Pos(#32, Cmd);
  if X > 0 then
  begin
    CmdLine := Copy(Cmd, X + 1, MaxInt);
    Cmd := Copy(Cmd, 1, X - 1);
  end;
  GetEnvironmentVariable('COMSPEC', CmdBUffer, SizeOf(CmdBuffer));
  if RunAProgram(Cmd, CmdLine, DefDir) = ERROR_FILE_NOT_FOUND then
  begin
    if RunAProgram(CmdBuffer, '/C' + Cmd + #32 + CmdLine, DefDir) = error_File_Not_Found then
    begin
      MessageBox(Application.Handle, PChar('File not found!'),
        PChar(Application.Title), MB_ICONWARNING + MB_OK);
    end;
  end;
end;
Best regards: olesio
Post Reply