I'm running some code in C# where I call a batch file, and I need to pass a Path with space as an argument and it does not work.
I have tried to call my argument different ways in the batch %1 , %~1, "%1", '%1'. None of these work. Also in my C# code I tried to convert it to string and it wont work either
C# code.
string argument = textBox10.Text.ToString() ;
string command = #"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat" + " " + argument;
System.Diagnostics.Process.Start("cmd.exe", command);
Batch code :
echo %1
Pause
When I pass an argument C:\Program Files\Test as a directory, it prints "C:\Program" and stops at the space.
How can I get the full path ?
Try this :
string command = #"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat"+ " \""+ argument +"\" ";
System.Diagnostics.Process.Start("cmd.exe", command);
This will "write" the following line in your console :
/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat "C:\Program Files\Test"
pass your argument double quoted, like so: "C:\Program Files\Test".
the issue is that your argument contains a space so it treated as two different arguments
It worked with simples quotes " '" + arg + "' ", and by calling it in the batch as %~1
Related
Well, before actually asking the question, I'll give you guys a brief description of what I'm trying to do. I wrote a few batches to install stuff here, and they work pretty well. The thing is... I want to write a program in C# that does the same thing as the batches. Most of what the batches do is call up files and fire them with parameters like /S or /silent. And, of course, activate Windows/Office.
But I'm having problems running the Office/Windows activators. Below, you'll see the sctructure of the batches we use and the C# program's structure as well.
#echo off
::Office Installation
:AskOffice
set INPUT=
set /P INPUT=Do you want to install Office 2010 (1), Office 2013 (2) or skip this step (3)? %=%
If /I "%INPUT%"=="1" goto 1
If /I "%INPUT%"=="2" goto 2
If /I "%INPUT%"=="3" goto eof
echo.
echo Invalid input & goto AskOffice
::Office 2010
:1
set INPUT=
set /P INPUT=Do you want to install Office (1) or just activate it (2)?
If /I "%INPUT%"=="1" goto instalar2010
If /I "%INPUT%"=="2" goto windows2010
:instalar2010
echo Installing Office 2010...
"\\jamaica\sistemas$\INSTALL\~SOFTWARES\Office\Office 2010\setup.exe" /config "\\jamaica\sistemas$\INSTALL\~SOFTWARES\Office\Office 2010\ProPlus.WW\config.xml"
goto windows2010
:windows2010
if defined ProgramFiles(x86) (
#echo You're running a x64 system...
goto 2010x64
) else (
#echo You're running a x86 system...
goto 2010x86
)
:2010x86
::Office 2010 Activation (x86)
echo Activating Office 2010 (x86)...
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office14\OSPP.VBS" /inpkey:XXXXXXXXX
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office14\OSPP.VBS" /act
goto eof
:2010x64
::Office 2010 Activation (x64)
echo Activating Office 2010 (x64)...
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office14\OSPP.VBS" /inpkey:XXXXXXX
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office14\OSPP.VBS" /act
goto eof
::Office 2013
:2
set INPUT=
set /P INPUT=Do you want to install Office (1) or just activate it (2)?
If /I "%INPUT%"=="1" goto instalar2013
If /I "%INPUT%"=="2" goto windows2013
:instalar2013
echo Installing Office 2013...
"\\jamaica\sistemas$\Install\~SOFTWARES\Office\Office 2013\setup.exe" /config "\\jamaica\sistemas$\Install\~SOFTWARES\Office\Office 2013\proplus.ww\config.xml"
goto windows2013
:windows2013
if defined ProgramFiles(x86) (
#echo You're running a x64 system...
goto 2013x64
) else (
#echo You're running a x86 system...
goto 2013x86
)
:2013x86
::Office 2013 Activation (x86)
echo Activating Office 2013...
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office15\OSPP.VBS" /inpkey:XXX
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office15\OSPP.VBS" /act
goto eof
:2013x64
::Office 2013 Activation (x64)
echo Activating Office 2013...
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS" /inpkey:XXXX
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS" /act
goto eof
:eof
This is my batch's code. All it does is ask which version of Office you'd like to install and then it activates it. Or, you can just activate it if you want. I want to do the same thing with C#, but using only C#. I could just create a method to fire up the batch file, but, well... I want to learn how to make CMD commands work in C#. Here's my C# class' code.
/* Office's installers' paths */
string varCaminhoOffice2010 = #"\\romenia\install$\~SOFTWARES\Office\Office 2010\setup.exe";
string varCaminhoOffice2013 = #"\\romenia\install$\~SOFTWARES\Office\Office 2013\setup.exe";
/* Local folders */
string varCaminhoOffice2010x86 = #"C:\Program Files\Microsoft Office\Office14\OSPP.VBS";
string varCaminhoOffice2010x64 = #"C:\Program Files (x86)\Microsoft Office\Office14\OSPP.VBS";
string varCaminhoOffice2013x86 = #"C:\Program Files\Microsoft Office\Office15\OSPP.VBS";
string varCaminhoOffice2013x64 = #"C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS";
/* Methods */
public void mtdAtivaOffice2010()
{
/* Office Activation */
if (mtdCheckArc == false) // Checking system's architecture
{
// x86
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x86 + "/inpkey:XXXX");
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x86 + "/act");
}
else
{
// x64
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x64 + "/inpkey:XXXX");
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x64 + "/act");
}
}
public void mtdAtivaOffice2013()
{
/* Office activation */
if (mtdCheckArc == false) // Checking system's architecture
{
// x86
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x86 + "/inpkey:XXXX");
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x86 + "/act");
}
else
{
// x64
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x64 + "/inpkey:XXXX");
System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x64 + "/act");
}
}
Everytime I try to run the project, Visual Studio gives me compilation error messages. I've tried a few things, and tried search the forums for help, but nothing helped me. I tried also:
Tried setting each command as a variable and then making the method run them:
string varCScript = #"%systemroot%\system32\cscript";
string varSerial2010 = "/inpkey:XXXX";
string varSerial2013 = "/inpkey:XXXX";
string varActivate = "/act";
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2010x86 + varSerial2010);
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2010x64 + varSerial2010);
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2013x86 + varSerial2013);
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2013x64 + varSerial2013);
Tried also inserting the wole command as a single string:
string varCommand = "%systemroot%\system32\cscript \"C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS\" /inpkey:XXXX";
I also tried adding more "\"s to this last line of code, between folders. Like "C:\\windows\\system32", but nothing works. Sometimes I get compilation erros, and sometimes my program runs... but when the CMD window opens, it flashes for a second and disappears. All I could read from one of them was "syntax problem". So, well... it looks like CMD isn't reading my strings properly. I mean, I'm not declaring them properly.
Could you guys help me with this one?
You need spaces between your parameters, and quotes around parameters which have spaces.
Also, to get some more info: put a breakpoint on the Process.Start line, get the script & arguments, and paste them into a cmd window.
What I got:
Process.Start("cmd.exe", "/K \"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"");
Even though I wrapped the filename with escaped ", it still displays the error:
'C:/Program' is not recognized as an internal or external command, operable program or batch file.
What is wrong?
You need to use two " for spaces in program path:
Process.Start("cmd.exe", "/K \"\"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"\"");
You code will be translated to
cmd.exe /K "C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js"
cmd.exe will translate it to
C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js
That's why it complain errors.
What you need is to enclose whole node.exe command with double quote again.
Process.Start("cmd.exe", "/K \"\"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"\""); so the node.exe command will be "C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js"
BTW, why don't just call node.exe directly?
Process.Start("C:/Program Files/nodejs/node.exe", "C:/rc/rainingchain/app.js");
I have to trigger another application after user done with my GUI. In order to trigger another application, i have to pass some command line argument to start that application. Let's say I have to pass below arguments:
c:\Program File\application.exe
-name text
-cmdfile c:\text\open.txt
The application.exe is a parameter that i want to pass to another applcation.
Before, that application set those argument in Visual Studio -> Properties -> Debug as "c:\Program File\application.exe" -name text -cmdfile c:\text\open.txt
As i understand, every space in above string consider as an argument except the one inside double quotes, so "c:\Program File\application.exe" is the first argument, -name is the second and text is the third. However, if i am using ProcessStartInfo.Argument property, if I set "c:\Program File\application.exe" -name text -cmdfile c:\text\open.txt, it first of all give an error, and than I add double quotes at the end of string, another application consider c:\Program as first argument and File\application.exe as second argument.
How can I avoid space as the separator for arguments? How can I pass the whole string as the same format setup in Visual Studio -> Properties -> Debug as "c:\Program File\application.exe" -name text -cmdfile c:\text\open.txt using ProcessStartInfo.Argument property?
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = "c:\Program File\application.exe" -name text
-cmdfile c:\text\open.txt
(error)
if
startInfo.Arguments = "c:\Program File\application.exe -name text
-cmdfile c:\text\open.txt"
it takes c:\Program as first argument and File\application.exe as second argument.
How can I figure this out? Any experience on that? Thank you in advance.
Your question is a little unclear. Do you want to start application.exe, or are you passing that as a parameter?
If you are starting it you can use:
var startInfo = new ProcessStartInfo(#"c:\Program File\application.exe");
startInfo.Arguments = #"-name text -cmdfile c:\text\open.txt";
Process.Start(startInfo);
If you are trying to start another process and want to pass application.exe as a parameter with the other parameters you could try:
var startInfo = new ProcessStartInfo("application2.exe");
startInfo.Arguments = #"""c:\Program File\application.exe"" -name text -cmdfile c:\text\open.txt";
Process.Start(startInfo);
I'm trying to run a fortran executable with Process.Start and it is not working.
Process proc = new Process();
string args = "<C:\\file.in> C:\\file.out";
proc.StartInfo = new ProcessStartInfo(AppName, args);
proc.Start();
if I paste those arguments into a command window the application runs as expected. proc.Start() does not run as expected.
Any ideas how I can view what Start is actually passing as arguments? My gut feeling is that this is a quotes issue.
The executable launches and hangs, so I'm confident the AppName is getting passed in correctly, it looks like an argument problem.
I tried setting the WorkingDirectory to that of the input and output files as suggested in this question: process.start() arguments but that did not work.
Redirection with the < and > command line operators is a feature that's implemented by the command line processor. Which is cmd.exe. Use its /c argument to execute just a single command:
string args = "/c " + AppName + " < C:\\file.in > C:\\file.out";
proc.StartInfo = new ProcessStartInfo("cmd.exe", args);
proc.Start();
Your args string is exactly what is being passed as arguments to the executable. You can double check it reading your Process ProcessStartInfo.Arguments Property.
Something similar happened to me once, i.e., calling the executable from the command line worked and from code didn't, and it turned out that when called from the command line the executable was running on my PC's [C:] drive, and when called from code it was running on my PC's [E:] drive, which was full!
To check which directory your application is using to run the executable use the Directory.GetCurrentDirectory Method.
I'm trying to run a command in cmd using C# and am having some difficulties. I'd like to be able to write the command to the cmd console so I can see what it's trying to run (I think there's some issue with the quotes or something, so if I could see the actual string in the command line, I'd be able to see exactly what the problem is). My code looks like this:
var processStartInfo = new ProcessStartInfo("cmd", "/c"+commandString);
processStartInfo.CreateNoWindow = true;
Process.Start(processStartInfo);
So basically, I just want to see the string commandString written in the console. Any help would be greatly greatly appreciated.
string CommandLineString = #"""C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe"" ""SELECT * FROM table where date >= '2009-01-01'"" queryout ""C:\Data\data.dat"" -S DBSW0323 -d CMS -n -T";
In this case, the problem is probably just your lack of a space after "/c".
var processStartInfo = new ProcessStartInfo("cmd", "/c " + commandString);
As for viewing in a command window, instead, you will probably be better off inspecting the Arguments property of your processStartInfo instance.
EDIT
Taking into account the command line details you posted, I believe this is what your issue is. Check out the following from cmd help:
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
the two quote characters
the string between the two quote characters is the name
of an executable file.
Since you are using /c, you have quote and special char issues still. Try wrapping your entire commandString in a set of quotes.
Take this simple example for instance (creating temp.txt manually of course):
string commandString = #"""C:\WINDOWS\Notepad.exe"" ""C:\temp.txt""";
var processStartInfo = new ProcessStartInfo("cmd", "/c " + commandString);
The command line to be executed will be: /c "C:\WINDOWS\Notepad.exe" "C:\temp.txt", but this will fail since "C:\temp.txt" is not an executable.
If you wrap the whole thing in one last set of quotes, you should see the intended result:
string commandString = #"""""C:\WINDOWS\Notepad.exe"" ""C:\temp.txt""""";
var processStartInfo = new ProcessStartInfo("cmd", "/c " + commandString);
Resulting in a command line of: /c ""C:\WINDOWS\Notepad.exe" "C:\temp.txt"" and ultimately opening notepad with your test file.
That string is not "written" to the console, it's part of the argument list for a program you launch (which in this case happens to be cmd.exe). Since the console created is owned by that program, unless it wants to print its arguments for its own reasons (which it won't) this is not directly doable.
If you simply want to debug then why not inspect the value of commandString, or write it out into a log file?
If you absolutely need the command line to be displayed in the console then you could resort to hacks (run another intermediate program that prints the command line and then calls cmd.exe with it), but unless there is some other good reason to use this approach I would not recommend it.