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;

相关阅读 >>

Delphi调用createprocess创建进程

Delphi integer.tryparse

老外写的在桌面添加快捷方式(Delphi xe5 android)

Delphi中利用钩子实现qq聊天窗口的修改

Delphi idftp发送本机ip和外网ip

Delphi 调用viewer-windows10 图像浏览器

Delphi 清空某个form的控件内容,用tag来判断

Delphi idhttp post 支持 gzip 解压缩

Delphi 用idhttp打开网页或下载文件时如何显示进度

Delphi webbrowser通过遍历id查找元素是否存在

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...