Member Avatar for TKSS

Hey all,


Just a quick C++ question from me...I'm sure this is info that is available on the web somewhere but a couple of searches weren't very fruitful.

Would it be possible for someone to provide an example of the following?

  • Calling/opening and executing a batch file from within the program.

What I want to do is call a batch file during a simple C++ program I'm writing for a class...pretty easy I'm sure, except the gay book we are using doesn't call files externally so I have no idea how to do it. A link to a page for help would suffice as well instead of an example. Thanks ahead of time.

Recommended Answers

All 7 Replies

The only standard method is the system function:

#include <cstdlib>
#include <iostream>

int main ( int argc, char *argv[] )
{
  if ( argc < 2 ) {
    std::cerr<<"Too few arguments"<<std::endl;
    return EXIT_FAILURE;
  }

  // Be ultra-anal
  if ( std::system ( 0 ) == 0 ) {
    std::cerr<<"No command processor available"<<std::endl;
    return EXIT_FAILURE;
  }
  
  return std::system ( argv[1] );
}
Member Avatar for TKSS

Ok...so forgive my silliness here...I've found something from the tutorials here at daniweb

#include <iostream>
#include <cstdlib> // You need this to call the system() function
using namespace std;

int main()
{
char prog[100];

cout << "Enter the program you want to call:\t"; // Prompt for a program and run it.
cin.getline(prog);

system(prog);

return 0; }

This will work as well right? If so...I'll need some advice. What I'm trying to do is call a batch script from within my program. Thus far, I've got it prompting the user to input a string of text. In the case of my program it will be a printer share...so the user will enter:

printer/share

Like that and the program stores the share name. After this, I'll have it prompt the user for the workstation NETBIOS name in string form and store that value.

The question is...How can I get these values to pass as parameters for the code(s) above to call that external batch file? I've hunted, and I still haven't seen anything that can point me in the right direction. Links or help is appreciated!

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string s = "mybatchfile ";
  string input;

  cout<<"Parameters: ";
  getline ( cin, input );

  system ( ( s + input ).c_str() );
}
Member Avatar for TKSS

Cool...that's looking like what I needed...I've got everything compiled and ready...but I don't think my second parameter is being passed to the batch script....if someone could take a glance below and point me toward the right solution, I'd be greatful

// begin program

#include <iostream> // required to perform C++ stream I/O
#include <cstdlib>  // INCLUDE standard library to call external files w/ one parameter
#include <string>  // to have the ability to deal with strings
#include <iomanip> // required for parameterized stream manipulation

using namespace std; // for accessing C++ Standard Library members

int main()
{

// define variables
	string printShare;
	string workStation;
	string parameters;

	cout << "\nWelcome to the Global Add Printer Script!";
	cout << "\nPlease Enter the NetBIOS/share name";
	cout << "\nof the workstation you're adding a printer to.";
	cout << "\nFor example:  WS-4SR-VS-08A/printername";
	cout << "\n\nENTER PRINTER NAME: ";
	getline( cin, printShare); // store user input in string form

	if ( printShare.size() == 0 )
	{
		cout << "\nError:  Please enter a share name." << endl;
	} // end if
	else
	{

	cout << "\nEnter this computer's";
	cout << "\nfull name: ";
	getline( cin, workStation);
	
	}

	if ( workStation.size() == 0 )
	{
		cout << "\nError:  Please enter a computer name." << endl;
	} // end if
	
	else 
	{	parameters = (workStation, printShare);

	  string s = "apwp.cmd ";
  

	  system ( ( s + parameters ).c_str() );

	} // end else

	return 0;  // indicate program is successful
   
} // End main

Here's what the batch file looks like:

rundll32 printui.dll,PrintUIEntry /ga /c\\%1 /n\\%2
	 sc \\%1 stop spooler
	 sc \\%1 start spooler

I get the following result

[img]http://linuxblog.sytes.net/downloads/docs/errorCpp.gif[/img]


Notice that after /n where %2 (second parameter) is supposed to go we find nothing passed to the batch script...or at least it looks that way. So, the important question is...was I correct using the following code?:

parameters = (workStation, printShare);

Thanks again for humoring me...I'm only in the first 9 weeks of learning how to code this stuff.

>parameters = (workStation, printShare);
This doesn't work like you think it does. The comma operator in C++ will evaluate each part, then return the last item. So that line of code is equivalent to tis:

workStation; // Do nothing
parameters = printShare;

If you want to add the two strings together and assign them to parameters, do just that:

parameters = workStation + ' ' + printShare;

I'm looking to include a .mov file, not a batch file, but I can't figure that out...is there anyone who can help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.