delphi 执行CMD并取得返回值  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi 执行CMD并取得返回值


delphi 执行CMD并取得返回值

function ExecuteCmds(Cmds: TStrings): String;
const
PRE_FIX = 'CJWTMPFILE';
var
i: Integer;
S,path,tmpFile,batFile: String;
temStrs: TStrings;

procedure waitFile(aFileName: string);
begin
while not FileExists(aFileName) do
begin
Application.ProcessMessages;
Sleep(100);
end;
end;

begin
Result := '';
for i := Cmds.Count - 1 downto 0 do
begin
S := Trim(Cmds);
if s = '' then
begin
Cmds.Delete(i);
Continue;
end;
Cmds := S + ' > ' + PRE_FIX + IntToStr(i) + '.txt';
end;
path := ExtractFilePath(ParamStr(0));
batFile := path + PRE_FIX + '.bat';
Cmds.SaveToFile(batFile);
ShellExecute(0,'open',PChar(batFile),nil,nil,0);
tmpFile := path + PRE_FIX + IntToStr(Cmds.Count - 1) + '.txt';
waitFile(tmpFile);
DeleteFile(batFile);
temStrs := TStringList.Create;
try
for i := 0 to Cmds.Count - 1 do
begin
tmpFile := path + PRE_FIX + IntToStr(i) + '.txt';
temStrs.LoadFromFile(tmpFile);
if Result <> '' then
Result := temStrs.Text
else
Result := Result + #13#10#13#10 +temstrs.Text;
DeleteFile(tmpFile);
end;
finally
temStrs.Free;
end;
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.050709962844849 seconds