Como obter o resultado de um comando DOS?

02/08/2008

1

Olá pessoal...

To querendo criar um sistema de Backup Restore, através do gbak, mas sendo que o sistema corrigirá altomaticamente o Banco de Dados sem nehuma intervenção nossa. O problema é o seguinte como eu obtenho o resulta do um comando DOS (GBAK) para saber se o banco de dados está ainda corrompido para o sistema partir dessa resposta ir para outra tarefa para tentar corrigi-lo, etc. Alguem pode me ajudar...


Responder

Posts

02/08/2008

Luciano.lirio

Max.jgs,

tenho uma aplicação que roda comandos DOS e retorna o resultado, se alguem tiver uma forma melhor tb quero..rs
a função Executa2 aguarda a finalização para retornar.

// Pega o dietório temporário do Windows function TempDir:String; var DirTemp:Array[0..Max_Path] of Char; begin GetTempPath(Max_Path,@DirTemp); Result:=StrPas(DirTemp); end; // Executa programas externos e espera finalizar function Executa(Arquivo: String; Estado: Integer): Integer; var Programa:array[0..512] of char; StartupInfo:TStartupInfo; ProcessInfo:TProcessInformation; Resultado:Cardinal; begin StrPCopy(Programa,Arquivo); FillChar(StartupInfo,Sizeof(StartupInfo),#0); StartupInfo.cb := sizeof(StartupInfo); StartupInfo.dwFlags := STARTF_USESHOWWINDOW; StartupInfo.wShowWindow := Estado; if not CreateProcess(nil,Programa,nil,nil,false,CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,nil,nil,StartupInfo,ProcessInfo) then Result := -1 else begin WaitForSingleObject(ProcessInfo.hProcess,Infinite); GetExitCodeProcess(ProcessInfo.hProcess,Resultado); Result := Resultado; end; end; function Executa2(Parametro: String; Estado: Integer = 0): TStrings; var Arq: TextFile; Bat: String; Ret: String; Linha: String; begin Result := TStringList.Create; Bat := TempDir+´Execute.bat´; Ret := TempDir+´Execute.Ret´; ReWrite(Arq,Bat); WriteLn(Arq,Parametro+´ > ´+Ret); CloseFile(Arq); Executa(Bat,Estado); Result.LoadFromFile(Ret); DeleteFile(Bat); DeleteFile(Ret); end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(Executa2(´copy c:\editor.zip c:\temp\xxx.zip´).Text); end; [code:1:dc8a3aadda][/code:1:dc8a3aadda]



Responder

02/08/2008

Luciano.lirio

Descupa aí, tinha mandado como citação..rs

// Pega o dietório temporário do Windows
function TempDir:String;
var
  DirTemp:Array[0..Max_Path] of Char;
begin
  GetTempPath(Max_Path,@DirTemp);
  Result:=StrPas(DirTemp);
end;

// Executa programas externos e espera finalizar
function Executa(Arquivo: String; Estado: Integer): Integer;
var
  Programa:array[0..512] of char;
  StartupInfo:TStartupInfo;
  ProcessInfo:TProcessInformation;
  Resultado:Cardinal;
begin
  StrPCopy(Programa,Arquivo);
  FillChar(StartupInfo,Sizeof(StartupInfo),0);
  StartupInfo.cb := sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Estado;
  if not CreateProcess(nil,Programa,nil,nil,false,CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,nil,nil,StartupInfo,ProcessInfo) then
    Result := -1
  else
    begin
      WaitForSingleObject(ProcessInfo.hProcess,Infinite);
      GetExitCodeProcess(ProcessInfo.hProcess,Resultado);
      Result := Resultado;
    end;
end;

function Executa2(Parametro: String; Estado: Integer = 0): TStrings;
var
  Arq: TextFile;
  Bat: String;
  Ret: String;
  Linha: String;
begin
  Result := TStringList.Create;
  Bat := TempDir+´Execute.bat´;
  Ret := TempDir+´Execute.Ret´;

  ReWrite(Arq,Bat);
  WriteLn(Arq,Parametro+´  > ´+Ret);
  CloseFile(Arq);

  Executa(Bat,Estado);
  Result.LoadFromFile(Ret);
  DeleteFile(Bat);
  DeleteFile(Ret);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(Executa2(´copy c:\editor.zip c:\temp\xxx.zip´).Text);
end;




Responder

02/08/2008

Max.jgs

Consegui mais três, total quatro agora vou estudalos e testalos para ve qual melhor vai me servir...


No exemplo abaixo deve ser incluído no form um componente Button, um componente StringGrid e um
componente ListBox.

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

// Evento OnClick do componente Button
procedure TForm1.Button1Click(Sender: TObject);

var
Env : PChar;
i : Integer;
S : String;
PosEq : Integer;
begin
Env := GetEnvironmentStrings;
With ListBox1,StringGrid1 do begin
While Env^ <> #0 do begin
Items.Add(StrPas(Env));
Inc(Env,StrLen(Env)+1);
end;
RowCount := Items.Count;
for i := 0 to Pred(Items.Count) do begin
PosEq := Pos(‘=’,Items[i]);
Cells[0,i] := Copy(Items[i],1,PosEq-1);
Cells[1,i] :=
Copy(Items[i],PosEq+1,Length(Items[i]));
end;
end;

end;



////////////////////////////////////////////////////////////////////////////

Questão/Problema:
Capturar informações do ambiente DOS
Resposta:
No exemplo abaixo deve ser incluído no objeto TForm um objeto do tipo Button, um objeto do tipo StringGrid e um objeto do tipo ListBox.


type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}

// Evento OnClick do objeto Button
procedure TForm1.Button1Click(Sender: TObject);
var
Env : PChar;
i : Integer;
S : String;
PosEq : Integer;
begin
Env := GetEnvironmentStrings;
With ListBox1,StringGrid1 do
begin
While Env^ <> #0 do
begin
Items.Add(StrPas(Env));
Inc(Env,StrLen(Env)+1);
end;
RowCount := Items.Count;
for i := 0 to Pred(Items.Count) do
begin
PosEq := Pos(‘=’,Items[i]);
Cells[0,i] := Copy(Items[i],1,PosEq-1);
Cells[1,i] :=
Copy(Items[i],PosEq+1,Length(Items[i]));
end;
end;
end;



//////////////////////////////////////////////////////////////////////////////////


to take input from InputFile and give output to OutputFile
Result : True on success
Parameters :
CommandLine : the command line for the app, including its full path
InputFile : the ascii file where from the app takes input
OutputFile : the ascii file to which the app´s output is redirected
ErrMsg : additional error message string. Can be empty
Error checking : YES
Target : Delphi 2, 3, 4
Author : Theodoros Bebekis, email bebekis@otenet.gr
Notes :
Example call : CreateDOSProcessRedirected(´C:\\MyDOSApp.exe´,
´C:\\InputPut.txt´,
´C:\\OutPut.txt´,
´Please, record this message´)
-----------------------------------------------------------------------------------}
function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile, ErrMsg :string):boolean;
const
ROUTINE_ID = ´[function: CreateDOSProcessRedirected ]´;
var
OldCursor : TCursor;
pCommandLine : array[0..MAX_PATH] of char;
pInputFile,
pOutPutFile : array[0..MAX_PATH] of char;
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
SecAtrrs : TSecurityAttributes;
hAppProcess,
hAppThread,
hInputFile,
hOutputFile : THandle;
begin

Result := False;

{ check for InputFile existence }
if not FileExists(InputFile)
then
raise Exception.CreateFmt(ROUTINE_ID + #10 + 10 +
´Input file * ¬s *´ + 10 +
´does not exist´ + 10 + 10 +
ErrMsg, [InputFile]);

{ save the cursor }
OldCursor := Screen.Cursor;
Screen.Cursor := crHourglass;

{ copy the parameter Pascal strings to null terminated strings }
StrPCopy(pCommandLine, CommandLine);
StrPCopy(pInputFile, InputFile);
StrPCopy(pOutPutFile, OutputFile);

TRY

{ prepare SecAtrrs structure for the CreateFile calls
This SecAttrs structure is needed in this case because
we want the returned handle can be inherited by child process
This is true when running under WinNT.
As for Win95 the documentation is quite ambiguous }
FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
SecAtrrs.nLength := SizeOf(SecAtrrs);
SecAtrrs.lpSecurityDescriptor := nil;
SecAtrrs.bInheritHandle := True;

{ create the appropriate handle for the input file }
hInputFile := CreateFile(
pInputFile, { pointer to name of the file }
GENERIC_READ or GENERIC_WRITE, { access (read-write) mode }
FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }
@SecAtrrs, { pointer to security attributes }
OPEN_ALWAYS, { how to create }
FILE_ATTRIBUTE_TEMPORARY, { file attributes }
0 ); { handle to file with attributes to copy }


{ is hInputFile a valid handle? }
if hInputFile = INVALID_HANDLE_VALUE
then
raise Exception.CreateFmt(ROUTINE_ID + #10 + 10 +
´WinApi function CreateFile returned an invalid handle value´ + 10 +
´for the input file * ¬s *´ + 10 + 10 +
ErrMsg, [InputFile]);

{ create the appropriate handle for the output file }
hOutputFile := CreateFile(
pOutPutFile, { pointer to name of the file }
GENERIC_READ or GENERIC_WRITE, { access (read-write) mode }
FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }
@SecAtrrs, { pointer to security attributes }
CREATE_ALWAYS, { how to create }
FILE_ATTRIBUTE_TEMPORARY, { file attributes }
0 ); { handle to file with attributes to copy }

{ is hOutputFile a valid handle? }
if hOutputFile = INVALID_HANDLE_VALUE
then
raise Exception.CreateFmt(ROUTINE_ID + #10 + 10 +
´WinApi function CreateFile returned an invalid handle value´ + 10 +
´for the output file * ¬s *´ + 10 + 10 +
ErrMsg, [OutputFile]);

{ prepare StartupInfo structure }
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdOutput := hOutputFile;
StartupInfo.hStdInput := hInputFile;

{ create the app }
Result := CreateProcess(nil, { pointer to name of executable module }
pCommandLine, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
True, { handle inheritance flag }
CREATE_NEW_CONSOLE or
REALTIME_PRIORITY_CLASS, { creation flags }
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo); { pointer to PROCESS_INF }

{ wait for the app to finish its job and take the handles to free them later }
if Result
then
begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
hAppProcess := ProcessInfo.hProcess;
hAppThread := ProcessInfo.hThread;
end
else
raise Exception.Create(ROUTINE_ID + #10 + 10 +
´Function failure´ + 10 + 10 +
ErrMsg);

FINALLY
{ close the handles
Kernel objects, like the process and the files we created in this case,
are maintained by a usage count.
So, for cleaning up purposes we have to close the handles
to inform the system that we don´t need the objects anymore }
if hOutputFile <> 0 then CloseHandle(hOutputFile);
if hInputFile <> 0 then CloseHandle(hInputFile);
if hAppThread <> 0 then CloseHandle(hAppThread);
if hAppProcess <> 0 then CloseHandle(hAppProcess);
{ restore the old cursor }
Screen.Cursor:= OldCursor;
END;

end;


Responder