Home > Net >  how to run consecutive commands in CMD when the first command starts a different instance (e.g. mysq
how to run consecutive commands in CMD when the first command starts a different instance (e.g. mysq

Time:11-29

I'm stuck on a problem where I can't find a reasonable work around. I'm trying to run multiple commands in the Windows CMD from inside a C Program using

CreateProcessW(NULL,L"mysql -u root -ptoor && source C:\Users\IEUser\Documents\SWE-Software\Datenbank\datenbank-build.sql", ... );

The Problem is that when i run this code (or even when I manually put in the the CMD Window, that the second half of the command won't run since prior to this it changes the "instance?" of the CMD to that of the mysql executable.

How would I fix or work around this?

I know in python you can somewhat simulate typing in a way, which could potentially be a work around here.. But i prefer a simple solution without any external libraries.

EDIT: To clarify the first Command seems to run since I can literally see the new console text of the mysql command. Its just that the second command won't be copied in there. It just stays blank and waits for input.

EDIT: This is the Output I'm getting: enter image description here

What I do want is this:

enter image description here

CodePudding user response:

Update:

  • It turns out there is another problem that masked what will become a follow-on problem with &&:

    • Your mysql -u root -ptoor command enters an interactive session, which requires manual exiting before processing continues.

    • Once you make this call non-interactive, you need to apply the cmd /c fix described below.

However, based on your latest feedback it appears that you mistakenly thought that you could send the mysql command string that starts with source ... to the interactive session via &&, which cannot work. If anything, you'd have to provide such a string via stdin (echo source ... | mysql ..., for which you'd need cmd /c too), though the better option is use arguments, as shown in this answerlink courtesy of drescherjm.


Since you're trying to use a shell operator, &&, you must call via that shell's CLI, i.e. via cmd /c on Windows:

CreateProcessW(NULL,L"cmd /c mysql -u root -ptoor && source C:\Users\IEUser\Documents\SWE-Software\Datenbank\datenbank-build.sql", ... );

As noted, && by design only executes its RHS command if the LHS one signaled success (via an exit code of 0).


As for what you tried:

Without cmd /c, all arguments starting with && are passed as additional, verbatim arguments to mysql.

  • Related