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 ?

Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
Destructor
  • 3,154
  • 7
  • 32
  • 51
  • possible duplicate of [How to run two commands in one line in Windows CMD?](http://stackoverflow.com/questions/8055371/how-to-run-two-commands-in-one-line-in-windows-cmd) – us2012 Sep 16 '13 at 10:55
  • Aren't you actually asking about passing parameters to an application through command line? – Dariusz Sep 16 '13 at 10:57
  • us2012: ya similar to that. Thanks :) Dariusz not parameters but two separate commands using same shell. – Destructor Sep 16 '13 at 17:20

2 Answers2

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.

Community
  • 1
  • 1
Arno
  • 4,994
  • 3
  • 39
  • 63
  • cool ! thanks ! and do you know how to avoid command prompt from popping up while using system() ? – Destructor Sep 16 '13 at 11:19
  • I've added information on how to resolve the newline issue, See my edit. – Arno Sep 16 '13 at 11:29
  • 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? – Destructor Sep 16 '13 at 11:34
  • Thanks a lot ! #ifdef _Win32 //Windows OS, use CreateProcess #else //Not windows This is correct ? – Destructor Sep 16 '13 at 12:26
0

You can achieve this using & separator:

set VAR=Hello & set VAR2=BYE
shofee
  • 2,079
  • 13
  • 30