{ "CMD.EXE /C " + '"' + "echo.exe test 123 4 5 6 7" + '"' + " " + "> " + '"' + @"C:\temp10\Pythontest.py" + '"' }
This would be the actual string:
CMD.EXE /c "echo.exe test 123 4 5 6 7" > "C:\temp10\Pythontest.py"
CMD has its own rules for processing command line arguments. This is the description from CMD help which can bee seen by typing CMD /?
at command prompt
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
If all of the following conditions are met, then quote characters
on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()@^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
Clearly your string is not covered by the case 1 so case 2 will applied to your command line string. That means CMD will remove the first and last quote after /C
switch before executing it. So CMD will try to execute this:
echo.exe test 123 4 5 6 7" > "C:\temp10\Pythontest.py
Now you see that the redirection operator is between quotes, It is now just a literal >
surrounded in quotes so no redirection is performed and this will be printed on console:
exe test 123 4 5 6 7" > "C:\temp10\Pythontest.py
Note that echo
is an internal command for CMD and windows does have any executable named echo.exe
. By using echo.exe
you are actually using internal echo
command which .exe
is it's argument. echo
always skips the first character so .
is eliminated and exe
will printed.
So this would be the correct string which work as intended:
CMD.EXE /c echo test 123 4 5 6 7 > "C:\temp10\Pythontest.py"
object[] theProcessToRun2 = { "CMD.EXE /C " + "echo test 123 4 5 6 7" + " > " + '"' + @"C:\temp10\Pythontest.py" + '"' };
Ok, got some enlightenment from a German forum.
The reason, why echo is not working in that case is:
echo tried to output it values to the stdout - which should be linked to a UI element. After the call is on a remote machine, there is no UI element who can be reached. So all the output is going into the Nirvana.
That's the reason, why it writes a file, but without content.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.