Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
All I need is to open a cmd prompt and pass commands to cmd prompt and run them.
For example
1.open cmd
2.pass a command to "change directory"
3.pass a command to open a file.

What I have tried:

I've tried as below:
C++
#include <iostream>
int main()
{
	std::cout << "Trying to run cmd using runas cmd";
	system("cmd;");

	return 0;
}


It opens cmd prompt but I need to pass commands to cmd and make them run.

I've tried as in run 2 or more cmd command using system() in C++ - Stack Overflow[^]

as system("cmd; cd/; d:;"); but raises error..Please help me..
Posted
Updated 10-Feb-16 17:14pm
v2

1 solution

The system() function already invokes the command processor cmd so that there is no need to execute it again if you not want to let the user enter commands or set specific options.

With Windows command prompts, using ';' to separate commands is not supported. Use '&' or '&&' instead where the latter will stop execution if a command returns with a non-zero value.

To check your commands open a Windows command prompt window and enter them. Once they do what you want pass the same string to the system() function.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Feb-16 11:15am    
5ed. I would also add: in a way, this is possible (there is a special command-line argument, /C), but makes no sense at all. The whole purpose of CMD.EXE it to carry out "commands" "without C++", by entering them on the console. Everything else can be done without it, as your first paragraph states. I cannot even explain why this question is so popular — probably this is a non-programming user's thinking.
—SA
Jochen Arndt 10-Feb-16 11:52am    
Thank you Sergey.

To extend what you have said:
When not using the /C option, each invocation of cmd has to be closed with the "exit" command.
7prasannaReddy 10-Feb-16 23:26pm    
Thanks and I've tried the below ones
system("cmd&&cd/");
system("cmd&cd/");
system("cmd & cd/");
system("cmd && cd/");
but no use. check and help me please.
Jochen Arndt 11-Feb-16 3:15am    
It seems you did not know what 'cmd' is doing when executing it in this context. So why you are using it?

You should also not reuse everything found on the web without getting the context. Your examples are for Unix/Linux which uses ';' as command separator. Another big difference between Unix and Windows is the path separator. With Windows it is the back slash '\'. So you must use that instead of the forward slash (here: 'cd \') because the forward slash is the command line option prefix.

Use a Windows command prompt to test your commands. The behaviour is similar and all your problems are related to that.

For example try

'dir /AD && cd \ && dir /AD'

It will list the directories in the current dir (your user directory when opening a new command prompt window), change to the root directory, and list the directories again (now for the root directory).

Now try the same with 'cmd':

'cmd && dir /AD && cd \ && dir /AD'

So what is cmd doing here?
Hint: Enter 'exit'.
7prasannaReddy 11-Feb-16 3:59am    
thanks

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