Batch file start appliction and pass multiple parameters switches spaces to á - c#

I created an c# wpf application that accepts command line parameters. If I open cmd and call the application with multiple parameters, the parameters are passed in correctly.
But if I do that same thing but from a batch file it passes the parameters as one parameter combined together rather then multiple parameters. I had the application output the parameters and it looks like all the spaces (which is what separates each command line parameter) were changed to a weird á character.
is there something special I need to do to get the parameters passed correctly?
I have tried resaving the file with ASCII encoding but that didn't change anything.
I also tried adding this line to the batch file
chcp 1253>NUL
that changed it so the á wasn't there but it still had it was one parameter.
seems like the spaces are just not getting passed as a space.
here is what my batch file line looks like, each parameter is separated by a space.
start /wait C:\MyTestApp.exe /SILENT /BOOLAGREEMENT=TRUE /BOOLGAOPTIN=TRUE
--UPDATE--Adding steps to reproduce...
this is just generic code similar to what I did just condensed
create c# wpf app.
in App.xaml.cs override OnStartup
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
bool shutdownapp = false;
MessageBox.Show(string.Join(",", e.Args));
}
build exe.
now launch cmd and cd to the location of the exe.
MyTestApp.exe /param1=test1 /param2=test2
you should get a message box that says
/param1=test1,param2=test2
now create a batch file that has something like this...then run it
test.bat
#echo off
start /wait c:\MyTestApp.exe /param1=test1 /param2=test2
this time the message box should have this...
/param1=test1/param2=test2

Start sees all of that as a CMD to Start.
Is there a reason you actually need to use start? generally, there isn't and you can just call the executable directly.
eg TestMyApp.cmd
#(
SETLOCAL
ECHO OFF
)
REM Call your Command here with all arguments:
"C:\MyTestApp.exe" /SILENT /BOOLAGREEMENT=TRUE /BOOLGAOPTIN=TRUE
If you sincerely require Start.
Then you should be aware that it treats all of that command as a single string, by nature, which is what you're running into, so you should be calling a new CMD instance explicitly instead in that case:
START "" /WAIT CMD /C ""C:\MyTestApp.exe" /SILENT /BOOLAGREEMENT=TRUE /BOOLGAOPTIN=TRUE"
But that is a lot of extra work to go through if not needed.
Alternatively, you can also just run a CMD instance directly:
CMD /C ""C:\MyTestApp.exe" /SILENT /BOOLAGREEMENT=TRUE /BOOLGAOPTIN=TRUE"
Or Use CALL:
CALL "C:\MyTestApp.exe" /SILENT /BOOLAGREEMENT=TRUE /BOOLGAOPTIN=TRUE

Related

'C:Program' not recognized as an internal or external command

Trying to send the code below to the command line, but I get errors. I know there is an issue with backslashes sent to CMD. Any help here on how to send it? Thanks!
string strCmdText="/C C:\\Program Files\\MetaTrader 5\\terminal64.exe /config:C:\\Users\\vguer036\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\config\\common.ini";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
When you use this:
string strCmdText="/C C:\\Program Files\\MetaTrader 5\\terminal64.exe /config:C:\\Users\\vguer036\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\config\\common.ini";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
then you are trying to start the application CMD.exe, which in turn you instruct to execute a particular application. However, because of the spaces, what CMD is trying to execute is the command C:\\Program with parameters Files\\MetaTrader, 5\\terminal64.exe etc. That is where your error message comes from.
One way to solve this is to add extra quotes around the filename (as Dour High Arch commented):
string strCmdText=#"/C ""C:\Program Files\MetaTrader 5\terminal64.exe"" ""/config:C:\Users\vguer036\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\config\common.ini""";
Note the doubled quotes, which are required to use a literal quote inside a verbatim string literal (#"...").
But this way you are still executing one application (CMD.exe) to start another (terminal64.exe). Why not start that terminal64 directly?
System.Diagnostics.Process.Start(
#"C:\Program Files\MetaTrader 5\terminal64.exe",
#"/config:C:\Users\vguer036\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\config\common.ini");
You should experiment to see whether you need extra quotes around that application name, but I don't think so.

redirecting output from an external dll in powershell

I have a C# assembly that processes an xml file and at the end spits out the results to the console.
e.g. Console.WriteLine(_header.ToString());
I can load this dll in powershell and call the right method like this:
[sqlproj_doctor.sqlprojDoctor]::ProcessXML($file) | out-file ./test.xml
All is well.
The problem begins when I want to redirect the output. For some reason stdout is empty. What am I missing? I need to be further process the output of this dll.
Note: If I compile the same code as an executable, it correctly populates the standard output stream and I can redirect the output.
another note: as a workaround, I changed the method from void to string, and can now manipulate the returned string.
When you call [Console]::WriteLine('SomeText'), it write to PowerShell process stdout, not to command output, and so it can not be redirected from inside same PowerShell process, by standard PowerShell operators, like this:
[Console]::WriteLine('SomeText')|Out-File Test.txt
You have to spawn new PowerShell process, and redirect output of that new process:
powershell -Command "[Console]::WriteLine('SomeText')"|Out-File Test.txt
In case if some command use [Console]::WriteLine to write to console, you can capture that output without starting new PowerShell instance:
$OldConsoleOut=[Console]::Out
$StringWriter=New-Object IO.StringWriter
[Console]::SetOut($StringWriter)
[Console]::WriteLine('SomeText') # That command will not print on console.
[Console]::SetOut($OldConsoleOut)
$Results=$StringWriter.ToString()
$Results # That variable would contain "SomeText" text.
Although this does not help if command does not use [Console]::WriteLine, but write to stdout stream directly:
$OldConsoleOut=[Console]::Out
$StringWriter=New-Object IO.StringWriter
[Console]::SetOut($StringWriter)
$Stdout=[Console]::OpenStandardOutput()
$Bytes=[Console]::OutputEncoding.GetBytes("SomeText"+[Environment]::NewLine)
$Stdout.Write($Bytes,0,$Bytes.Length) # That command will print on console.
$Stdout.Close()
[Console]::SetOut($OldConsoleOut)
$Results=$StringWriter.ToString()
$Results # That variable will be empty.

Pass parameters with apostrophs to a batch file

How do I passt following parameters to my batch file?
custom.bat mode="test" logs="true"
I tried to double the " but nothing helped.
custom.bat "mode="test"" "logs="true""
And, in custom.bat you remove the unneeded quotes
#echo off
set "arg1=%~1"
set "arg2=%~2"
echo [%arg1%] [%arg2%]
You may use CALL command to launch a new batch-file. After executing the last line of the "called file", the control will return back to the "calling file".
You may set the parameters to the "called .bat fie" by using either a simple string or a variable.
eg.
CALL MyScript.bat "1234"
or
SET _MyVar="1234"
CALL MyScript.bat %_MyVar%
As a precaution, you may use SETLOCAL & ENDLOCAL to keep separation between variables of same-name among different files.

Stop Command Prompt from closing so quickly

I'm trying to troubleshoot why the following function isn't working.
public void RunCmd()
{
string strCmdText;
strCmdText = "/C [enter command stuff here]";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
However whenever I try to run it or throw in some breakpoints, command opens, shows an error, and then closes really quickly (so quickly that I can't read anything).
Is there a way I can halt the program or figure out what's going on? Breakpoints don't seem to be working.
When I directly type it in Command Prompt instead of running it via this c# script, it the command works fine.
try this:
strCmdText = "/K [enter command stuff here]";
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
Maybe try adding a pause command?
There are a various options. Using /K will prevent the window from closing.
You can also edit your command to add a SLEEP after the main call. For example, the following will wait 2 seconds before exiting:
public void RunCmd()
{
string strCmdText = "/C \"[enter command stuff here]\" & \"SLEEP 2\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
Process.Start has two parameters there: the process name and the arguments to pass to the process. CMD is having a problem understanding what the argument "[enter" is. If it is not an argument CMD understands, it will immediately exit.
cin.get()
That waits for a keyboard press from the user.
create temp folder under c: then append " < c:\temp\log.txt" to the end of command. this writes the output to a file
In addition to the tip about using /K vs /C to create a shell that 'lingers', you'll probably want to inject an empty "set" command to see what environmental variables (and paths) are set in the spawned shell. In all likelihood, the difference between the successful run of your script in your own shell session and the one that fails in the spawned shell, is the environment settings in which it is run.

CMD command with space in path

I am having an issue with the spaces in my command. I am trying to run the cmd prompt and execute a program that takes command line arguments. I need the cmd window to remain open after the process is finished executing.
I managed to get it working in another section of code, but this time i am almost sure it has to do with the spaces in the path of the argument. If i use a path with no spaces, it works fine. I tried to escape the quotes, but either i am doing it incorrectly, or escaping the quotes do not work.
Basically, I need to make the line below work with spaces and keep the cmd window open after the execution...
Dim ps As Process = System.Diagnostics.Process.Start("cmd /k", "C:\common\tools\tap.exe -f flash C:\Users\test project\Desktop\image.signed")
I'm know the space between "test" and "project" is the issue, but i haven't been able to get around it.
Thanks in advance for your help.
Wrap the path in double-quotes, like this:
"C:\common\tools\tap.exe -f flash ""C:\Users\test project\Desktop\image.signed"""
Giving you:
Dim ps As Process = System.Diagnostics.Process.Start ("cmd /k", "C:\common\tools\tap.exe -f flash ""C:\Users\test project\Desktop\image.signed""")

Categories