Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm running a batch file from C++ code. system("sample.bat"). My question is, when I finish my C++ program and turn it into an exe. Is there a way to include the bat file so that it is included in the program? Thanks.
Posted

One idea is to add it as a (text-) resource in your exe. Then either execute it row by row if possible, or write it to a temporary file and run it from there.
 
Share this answer
 
Comments
Member 7766180 26-Sep-11 16:59pm    
Thats sounds really good!!!! Thank you!!!
If you are using batch file for running any application you can write code in your program for corresponding operation

Or you can create a deployment setup with this batch file and create it as a setup file to install to other machines
 
Share this answer
 
Comments
Member 7766180 26-Sep-11 12:34pm    
This is a batch file that deletes files. I just need to include this file with the c++ program so that when I make the c++ program an actual exe. the batch file is in there.
Albert Holguin 26-Sep-11 17:31pm    
Deleting files shouldn't be a big deal in C++, plus including a batch file as a resource will increase the size of your exe for no good reason. Anyway, Niklas' suggestions were good but for what you say you're doing, I'd recommend just adding a function in C++ to take care of that.
Member 7766180 26-Sep-11 17:56pm    
Thank you Albert. But from every angle the problem is that I cant find a file somewhere on the hard drive that is less than 2 minutes old using c++. I dont know the name or location only the estimated time of the file and it seems a batch can do it.
In general calling system from a program to execute system commands is not a "good practice": there is no way to be granted that the system really do what you asked, especially in the case someone has messed it up replacing commands with other programs.

Basically, every call to "system" is an external hook virus writers can use to attach their own actions.

There are C library fuctions to manage files, as well as operating system native APIs. You should rely on them.

Of course, all that needs to be contextualized: if your program runs in a limited and controlled environment, (where it can be assumed you know everything the system will do and can be granted it will not do anything else) that can be done.
 
Share this answer
 
v2
Comments
Member 7766180 27-Sep-11 2:55am    
Thank you Emilo. System is not the way I want to go. I have a C++ program running that upon occasion calls a batch file. I need the main program to continue to run while the batch file executes. However there is a strong possibilty that the batch file will have seveal instances running at once, I need them to do so without interfering with the main program.

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