procedure button1Onclick(.....);
var
  sPath:string;
  mybat:textfile;
begin
  sPath:=当前工程路径;  assignfile(mybat,spath+'a.bat');
  rewrite(mybat);
  writeln(mybat,'ping 127.0.0.1');
  closefile(mybat);
  winexec(spath+'a.bat',sw_hide);  //再对mybat重写一次
  assignfile(mybat,spath+'a.bat');
  rewrite(mybat);  //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
  //如果前面没运行过,就正常重写了a.bat中的内容
  writeln(mybat,'ping 192.168.1.3');
  closefile(mybat);
  winexec(spath+'a.bat',sw_hide);end;想问 用同一个.bat文件 怎么运行多次?????????

解决方案 »

  1.   

     winexec(pchar(spath+'a.bat'),sw_hide);
    语句都试过了,没有问题
    要是没执行过winexec
    那么重写a.bat后,内容也变化了
    就是执行一次后,执行到rewrite那里就i/o异常了 
      

  2.   


    procedure TForm1.btn2Click(Sender: TObject);
    var
     s1: TStringList;
     s2:TMemoryStream;
     s:String;
     i:integer;
     sPath:string;
    begin
      sPath:='当前工程路径';
      s1 := TStringList.Create;
      s2:=TMemoryStream.Create;
      if fileexists(sPath + 'a.bat') then
      begin
        s1.LoadFromFile(sPath + 'a.bat');
      end;
      try
        s:=Trim(Edit1.Text);{可变内容}
        s1.add(s);
        s1.SaveToStream(s2);
        s2.SaveToFile(sPath + 'a.bat');
      finally
        s1.free;
        s2.Free;
      end;
     end;
      

  3.   

    楼上的并没有执行winexec语句呀.
    我想知道怎么,才能执行了一次winexec后
    再对mybat重写内容,能够再执行winexec.因为程序中,要不断对mybat重写后,再执行呀.急急急!!!
      

  4.   

    我这样多次执行没有产生你所说的错误:procedure TForm1.Button1Click(Sender: TObject);
    var
      sPath:string;
      mybat:textfile;
    begin
      sPath:=ExtractFilePath(ParamStr(0));
      assignfile(mybat,spath+'a.bat');
      rewrite(mybat);
      writeln(mybat,'ping 127.0.0.1');
      closefile(mybat);
      winexec(pchar(spath+'a.bat'),sw_hide);
      assignfile(mybat,spath+'a.bat');
      rewrite(mybat);
      writeln(mybat,'ping 192.168.1.3');
      closefile(mybat);
      winexec(pchar(spath+'a.bat'),sw_hide);
    end;
      

  5.   


    Procedure TForm1.Button1Click(Sender: TObject);
    Var
      sPath: String;
      mybat: textfile;
    Begin
      sPath := ExtractFilePath(Application.ExeName);  assignfile(mybat, sPath + 'a.bat');
      rewrite(mybat);
      writeln(mybat, 'ping 127.0.0.1');
      closefile(mybat);
      winexec(PChar(sPath + 'a.bat'), sw_hide);  //再对mybat重写一次
      assignfile(mybat, sPath + 'a.bat');
      rewrite(mybat); //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
      //如果前面没运行过,就正常重写了a.bat中的内容
      writeln(mybat, 'ping 192.168.1.3');
      closefile(mybat);
      winexec(PChar(sPath + 'a.bat'), sw_hide);
    End;
      

  6.   

    var
      sPath:string; 
      mybat:textfile;
    begin
      sPath:=ExtractFilePath(ParamStr(0));  assignfile(mybat,spath+'a.bat');
      rewrite(mybat);
      writeln(mybat,'ping 127.0.0.1');
      closefile(mybat);
      winexec(PChar(spath+'a.bat'),1);
      Sleep(3000); //注意要加上停顿时间 
      //再对mybat重写一次
      assignfile(mybat,spath+'a.bat');
      rewrite(mybat);  //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
      //如果前面没运行过,就正常重写了a.bat中的内容
      writeln(mybat,'ping 192.168.1.3');
      closefile(mybat);
      winexec(pchar(spath+'a.bat'),1);
      

  7.   

    用下面的函数看看 function TForm1.WinExecAndWait32(FileName:String;Visibility:Integer;var mOutputs:string):Cardinal;
    var
      sa:TSecurityAttributes;
      hReadPipe,hWritePipe:THandle;
      ret:BOOL;
      strBuff:array[0..255] of char;
      lngBytesread:DWORD;
      
      WorkDir:String;
      StartupInfo:TStartupInfo;
      ProcessInfo:TProcessInformation;
    begin
      FillChar(sa,Sizeof(sa),#0);
      sa.nLength := Sizeof(sa);
      sa.bInheritHandle := True;
      sa.lpSecurityDescriptor := nil;
      ret := CreatePipe(hReadPipe, hWritePipe, @sa, 0);  WorkDir:=ExtractFileDir(Application.ExeName);
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb:=Sizeof(StartupInfo);
      StartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      StartupInfo.wShowWindow:=Visibility;  StartupInfo.hStdOutput:=hWritePipe;
      StartupInfo.hStdError:=hWritePipe;  if not CreateProcess(nil,
        PChar(FileName),               { pointer to command line string }
        @sa,                           { pointer to process security attributes }
        @sa,                           { pointer to thread security attributes }
        True,                          { handle inheritance flag }
    //    CREATE_NEW_CONSOLE or          { creation flags }
        NORMAL_PRIORITY_CLASS,
        nil,                           { pointer to new environment block }
        PChar(WorkDir),                { pointer to current directory name, PChar}
        StartupInfo,                   { pointer to STARTUPINFO }
        ProcessInfo)                   { pointer to PROCESS_INF }
        then Result := INFINITE {-1} else
      begin
    //    Form1.Hide;
    //    FileOpen(FileName,fmShareExclusive);
    //    SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
        ret:=CloseHandle(hWritePipe);
        mOutputs:=';
        while ret do
        begin
          FillChar(strBuff,Sizeof(strBuff),#0);
          ret := ReadFile(hReadPipe, strBuff, 256, lngBytesread, nil);
          mOutputs := mOutputs + strBuff;
        end;    Application.ProcessMessages;
        WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
        GetExitCodeProcess(ProcessInfo.hProcess, Result);
        CloseHandle(ProcessInfo.hProcess);  { to prevent memory leaks }
        CloseHandle(ProcessInfo.hThread);
    //    Form1.Close;                        { exit application }
        ret := CloseHandle(hReadPipe);
      end;
    end;
      

  8.   

    如VIP_Rainbow所说,两次执行之间,加个停顿就可以
    有没有方法,让它一执行完第一次winexec后,马上就执行下面的语句????
      

  9.   

    这个肯定是因为你的bat文件里有一些文件没有执行完毕,cmd会占用bat文件,就是说文件在使用中,建议你在bat中采用start 命令来启动一些不知道啥时候结束的文件,这样就不会有这样的事情发生了
      

  10.   

    用WaitforSingleObject等待bat退出后再rewrite。如8楼那样~~~另外,为什么不直接执行需要的程序,反而写入bat文件再执行?
      

  11.   

    前一个还没执行完, .bat文件处于打开状态 ?