I am writing a small shell script for windows just for fun and was wondering how to open executable programs? Can c++ do this?

C++ can do anything which you could do from the command prompt, using the system() function - eg, (Assuming some version of DOS or Windows)

system("dir"); //DOS Command to show a directory listing
system("pause");  //DOS command to make a pause happen

Yup, use the system() command. For example:

system("DIR C:\");

lists the contents of the C:\ drive. Simply enter in the path of the script, and you're good to go.

Hope this helps

[edit]Too slow...[/edit]

thanks!

Presumably you meant

system("DIR C:\\");

I forgot! My bad. I guess it's because I'm so used to using forward slashes for Unix that I forgot about the needed escape charecter under Windows. But I heard somewhere that you can still use forward slashes in C++ programs under Windows; correct?

my command prompt in Win2K doesn't like forward-slashes when used with commands (such as ' dir c:/ ' - the command complains that i'm using an invalid switch ). Although its ok when specifying the path of an executable - eg ' c:/winnt/explorer.exe '

But I heard somewhere that you can still use forward slashes in C++ programs under Windows; correct?

Yep, actually using forward slashes is the most portable way of putting slashes inside commands such as system as well as while hardcoding file path, rather than using \\ which works only on MS systems.

my command prompt in Win2K doesn't like forward-slashes when used with commands (such as ' dir c:/ ' - the command complains that i'm using an invalid switch ). Although its ok when specifying the path of an executable - eg ' c:/winnt/explorer.exe '

Thats because the forward slash is actually not required for the purpose you are trying to achieve. You can do system( "dir c:" ) and get the same results.

Thats because the forward slash is actually not required for the purpose you are trying to achieve. You can do system( "dir c:" ) and get the same results.

That was a bad example. dir C:/WinNT might be a better one :)

That was a bad example. dir C:/WinNT might be a better one :)

Well, it you are so well aware you are using a MS windows OS along with the fact that there is no command named dir in *nix, it is better if you use dir c:\winnt . ;)

Is there a way to start program not via dos? Im writng a shell script after all...it's kind of cheating...

Is there a way to start program not via dos? Im writng a shell script after all...it's kind of cheating...

A shell script is just a file that batches shell commands together so that you don't have to constantly retype them. If doing things via the shell is cheating then all shell scripting is cheating. ;)

Is there a way to start program not via dos? Im writng a shell script after all...it's kind of cheating...

DOS has nothing to do with it. Nor does the 'command prompt' which is often incorrectly referred to as 'DOS' :)

What you're doing by calling system() is using the OS's command interpreter (Windows Command prompt is merely a commandline interface for the Windows command interpreter). The Windows command interpreter does exactly the same thing regardless of whether you're calling from Command prompt, from a C++ program, from the "run" box in the start menu, from a shortcut icon on the desktop.......

There's probably another way to do what you want which doesn't involve the OS's command interpreter, but for that you'll have to find some library with your compiler, or search around for some API which directly interacts with the OS, and bypasses the command interpreter altogether. If you want the easy option, use the system() function :)

Agreed. Though I am no expert on such things but here it goes.

Command line interface via the console window and actual system functions are altogether different things. The former just provides an interface for the user of admin to type commands in which are then executed by the latter. These commands are known as system or shell commands. These commands in turn tap into the core functionality of what is known as the kernel.

When you type in console window:

command line interpreter -> invoke system commands -> tap into kernel

Whereas when calling from programs:

hardcoded string -> invoke system commands -> tap into kernel

So I wouldn't regard it as cheating, but just a logical way of invoking the system commands...

Although you must keep in mind that using system() commands usually takes away all portability from your program as previously mentioned. When programming, try to hardcode the functions right into your program instead of relying on prewritten tools. Although it may seem easier at the time, you'll thank yourself afterwards if you wrote a program that's completely portable.

If you simply must use system() calls, one way to keep it portable is to use flags to use different commands to make it portable. For example, if you want to clear the screen:

#if defined TARGET_WINDOWS
system("CLS");

#elseif defined TARGET_UNIX
system("clear");
#endif

And so on.

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.