We help IT Professionals succeed at work.

Execute .bat file

stain
stain asked
on
I need to write a program that can run in the background and execute a .bat file within a unit amount of time, over and over.  The main concern is having this programm execute a .bat file, how do I write a program to execute another?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
You could just use

BOOL ExecuteAndWaitForCompletion ( LPCTSTR pszCmd)
{
      STARTUPINFO si;
      PROCESS_INFORMATION pi;

      ZeroMemory ( &si, sizeof ( STARTUPINFO));

      si.cb = sizeof ( STARTUPINFO);
      si.dwFlags = STARTF_USESHOWWINDOW;
      si.wShowWindow = SW_HIDE;

      BOOL bRes     =     CreateProcess     ( NULL,
            (LPTSTR)pszCmd,
            NULL,
            NULL,
            TRUE,
            NORMAL_PRIORITY_CLASS,
            GetEnvironmentStrings(),
            NULL,
            &si,
            &pi
            );

      WaitForSingleObject     (     pi.hProcess,     INFINITE);

      CloseHandle( pi.hProcess);
      CloseHandle( pi.hThread);

      return ( bRes);
}


and call it like

ExecuteAndWaitForCompletion ( "cmd.exe /c c:\\mypath\\mybat.bat");

(replace 'cmd.exe' with 'command.com' on Win9x/ME)
I would use ShellExecute() for .BAT files.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.