Click here to Skip to main content
15,033,917 members
Please Sign up or sign in to vote.
2.50/5 (3 votes)
See more:
I want to execute .bat file from the cpp code. Can any one suggest me the code to run .bat file from the code in windows 32.
Posted
Updated 22-Aug-13 16:08pm
v2
Comments
[no name] 22-Aug-13 21:17pm
   
You did not say for what platform so for Windows, http://msdn.microsoft.com/en-us/library/windows/desktop/bb776886%28v=vs.85%29.aspx
Sergey Alexandrovich Kryukov 22-Aug-13 22:06pm
   
As to ".bat file"... Not Windows? Well, not very likely... :-)
Anyway, this link makes a correct answer.
—SA
Michael Haephrati 23-Aug-13 18:37pm
   
It says "Window 32"

There are many ways for starting another executable from a Windows application:

(of course there are few other ways...)
The linked documentation pages provide code examples.
   
Less than a minute of googling found me this:
http://www.cplusplus.com/reference/cstdlib/system/[^]
   
You can use ShellExecute[^] if in Windows
   
I would suggest using ShellExecute.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx[^]

HINSTANCE ShellExecute(
_In_opt_ HWND hwnd,
_In_opt_ LPCTSTR lpOperation,
_In_ LPCTSTR lpFile,
_In_opt_ LPCTSTR lpParameters,
_In_opt_ LPCTSTR lpDirectory,
_In_ INT nShowCmd
);

The lpOperation parameter should be "OPEN" and then the lpFile should be the batch file.
   
I did it myself:
process proc;
proc->argument="krun.bat";
proc->filename="cmd.exe";
proc->workingdirectory="folder path";
proc->start();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900