1

how can I run multiple commands at once from windows command line? i want to set a couple of env variables and use them in the c++ program. Like:

set VAR=Hello and set VAR2=BYE

and same program should do:

echo %VAR% and echo %VAR2%

and the output should be:

Hello BYE

How to achieve this in c/c++ ? any way to do this using system() function ?

3
  • possible duplicate of How to run two commands in one line in Windows CMD? Commented Sep 16, 2013 at 10:55
  • Aren't you actually asking about passing parameters to an application through command line? Commented Sep 16, 2013 at 10:57
  • us2012: ya similar to that. Thanks :) Dariusz not parameters but two separate commands using same shell. Commented Sep 16, 2013 at 17:20

2 Answers 2

1

You may execute a number of shell commands with the & seperator:

echo %VAR% & echo %VAR2%

See this SO answer to get more details.

Edit:

Unfortunately this will put the output seperated in two lines.

Hello

BYE

However, there is a solution for that too:

SET /P Var=%VAR%<NUL & echo %VAR2%

will output

Hello BYE

Edit 2:

Do not use system(), better use the CreateProcess function which allows you to set creation flags like CREATE_NO_WINDOW.

Sign up to request clarification or add additional context in comments.

4 Comments

cool ! thanks ! and do you know how to avoid command prompt from popping up while using system() ?
I've added information on how to resolve the newline issue, See my edit.
but still the command prompt window pops up na? how to avoid it? i want the output to be read just by my program, console should not appear . anyway to do this?
Thanks a lot ! #ifdef _Win32 //Windows OS, use CreateProcess #else //Not windows This is correct ?
0

You can achieve this using & separator:

set VAR=Hello & set VAR2=BYE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.