Start external program with arguments (in Visual Studio - Debug properties) - visual-studio

Is it possible to run program with parameters, like on screenshot (Specifically, I've tried to run CMD with some params):
It gives me an error while I am trying to do that.

You should specify parameters in the Command line arguments box:

The problem might be stemming from the second path in your argument. Try wrapping whatever the C:\Program Fi (it's cut off in the screenshot) path is in quotes.
C:\windows\system32\cmd.exe start /m "" "C:\Program Files(x86)\Path\To\Directory"

Related

How can I get Visual Studio to use /RunExit and provide my programs command line arguments?

I have a solution file for a command line executable. I want to run my executable, with different inputs, through the debugger without my interaction, while also setting its output to a log file.
For example, this is sort of what I want:
devenv /DebugExe "myprogram.exe" "my inputs"
That loads VS and automatically sets my programs inputs. However, I want to do this over and over with different inputs to my program and mine the output files later. So the closest I've figured out, but doesn't work, is this:
devenv /RunExit "myprogram.exe" "my input set" /Out out1.log
devenv /RunExit "myprogram.exe" "a different input set" /Out out2.log
...
Is there any way to do this? Again, the important part is that I could queue up a bunch of runs and mine the output files later for their output.
While I did find a way to do what I want, I don't like it. So I'll wait a while before marking my own answer as accepted.
What I really needed and wanted was what I stated in my question:
devenv /RunExit sln "input args" /Out out.log
The problem is that VS doesn't allow this, "input args" is invalid - unlike if you were to use say /DebugExe but then there is manual work involved again and that didn't help me. So in the script I'm using to call devenv dynamically, I used a regex to replace the "Arguments = " line in the sln file with the appropriate arguments each time. Then this command line works:
devenv /RunExit sln /Out out%x%.log
Each call the sln is modified to contain the new set of args and so each run, I'll get different output in my out%x%.log file (which I name differently each run so I can keep track of which log file went to which inputs). Thanks everybody for watching.
I wanted to do something similar: in my case one of my parameters was a file system path, which could contain a space, which would have to be quoted, inside the string in the batch file which must be quoted. I enhanced my command line executable's code to also look at environment variables (Environment.GetEnvironmentVariable) in addition to the command line parameters to it. Then just set the specific environment variable value prior to each invocation of devenv.

Administrator's shortcut to batch file with double quoted parameters

Take an excruciatingly simple batch file:
echo hi
pause
Save that as test.bat. Now, make a shortcut to test.bat. The shortcut runs the batch file, which prints "hi" and then waits for a keypress as expected. Now, add some argument to the target of the shortcut. Now you have a shortcut to:
%path%\test.bat some args
The shortcut runs the batch file as before.
Now, run the shortcut as administrator. (This is on Windows 7 by the way.) You can use either right-click -> Run as Administrator, or go to the shortcut's properties and check the box in the advanced section. Tell UAC that it's okay and once again the shortcut runs the batch file as expected.
Now, change the arguments in the target of the shortcut to add double quotes:
%path%\test.bat "some args"
Now try the shortcut as administrator. It doesn't work this time! A command window pops up and and disappears too fast to see any error. I tried adding > test.log 2>&1 to the shortcut, but no log is created in this case.
Try running the same shortcut (with the double quotes) but not as Administrator. It runs the batch file fine. So, it seems the behavior is not because of the double quoted parameters, and it's not because it's run as Administrator. It's some weird combination of the two.
I also tried running the same command from an administrator's command window. This ran the batch file as expected without error. Running the shortcut from the command window spawned a new command window which flashed and went away. So apparently the issue is caused by a combination of administrator, the shortcut, and the double quotes.
I'm totally stumped, does anyone have any idea what's going on? I'd also like a workaround.
I just ran Process Monitor on this and here is what I saw:
Run as User:
cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""
Run as Admin:
"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"
For some reason, the Run as Admin case is not quoting the entire command. It seems it is trying to run the command:
C:\Users\Sunbelt\Desktop\test.bat" "some args
I would guess that since the first space is quoted it actually trying to run the following command:
"C:\Users\Sunbelt\Desktop\test.bat some" args
And in Process Monitor logs there is a file system entry of "NO SUCH FILE" for "C:\Users\Sunbelt\Desktop\test.bat some". I don't know why it is different when run as Admin, but that's what appears to be happening.
To work around this, create another bat file on a path without spaces, and with a filename without spaces, that simply does this:
call "Long path to original bat file\Original bat file.bat"
This secondary bat file can be run as admin.
You can now create a shortcut to this secondary bat file and check run as admin in the shortcut's advanced options. The shortcut can be placed on a path with spaces and it can have a filename containing spaces.
In my case I just want to pass one filename as parameter, but the path has spaces.
I found a solution that worked for this case (if that's okay to truncate the file name).
Create another bat file (input_gate.bat) to remove the spaces in the path using the syntax of CALL.exe.
Assuming that the shortcut is named test.lnk and is on the same route as the input_gate.bat:
call %~sdp0test.lnk %~sf1
This pass as a parameter to test.bat the full file name in short format, with administrator privileges.
%~sdp0 -> Is the current path (for the input_gate.bat) in short format.
%~sf1 -> Is the first parameter passed to input_gate.bat (in my case the full filename with spaces)
This worked for me in Windows 7:
ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn: "C:\Path containing\my script"
Not tried it yet as Admin. I don't think it would work if myscript.bat contained spaces
I finally figured it out, in a way that allows the use of long filenames (short filenames weren't adequate for my use case). My solution works on Win 7, 8, and 10. I think it was inspired by Luke's mention of the missing double-quote.
Here it is:
1.) For the shortcut you create to the batch file, which you set to run as admin, use the following command line:
cmd /s /c ""path_to_batch_file"
Note that there are 2 double-quote characters at the beginning of the command, and only 1 at the end, even though there should normally be 2 at the end also. But that is the trick in getting it to work!
2.) In your batch file, add back the missing double-quote character:
set user_file=%1"
That's it! Here's an example to let you see it in action:
1.) On your desktop, create "test.bat" with the following content:
#echo off
set user_file=%1"
echo The file is: %user_file%
pause
3.) Create a shortcut to the batch file. Set it to run as admin, and give it the following command line:
cmd /s /c ""%userprofile%\desktop\test.bat"
4.) Drag a file onto the shortcut. Success! (I hope...)
Answer here worked for me: https://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin
Which is...
Adding cmd /C before the target.
I also had to make sure my script's name and path didn't have spaces, and not quote the script's path in target.

Double-Quotes in start line of Windows Batch script

I have looked at the answers already provided, but I'm still stuck. Here is what I currently have:
start "" "C:\Program Files (x86)\Spark\Spark.exe"
echo Spark started
This works fine. But now I want to pass parameters to the client, which must be wrapped in quotes. I can do the following on the command line:
"C:\Program Files (x86)\Spark\Spark.exe" "user=%USERNAME%&server=example.org"
And it starts up with the user and server fields filled in.
But when I try to edit the batch script to add those quote-wrapped parameters, I get a variety of errors depending on how I try to add double-quotes and where, etc.
So how would I add a quote-wrapped parameter to the start line?
Update:
I accidentally got it to work, but haven't been able to reproduce it. But it didn't work exactly right. The user name was still blank, but the server was filled in. I forgot to mention that I am using an environment variable for the user name: %USERNAME%
So it might be my problem is that I can't escape quotes and use environment variables?
Final Answer:
It turns out that part of the problem was that I was using the wrong parameter, but originally I had been using the right one, so I didn't notice. From the command line, I should have:
"C:\Program Files (x86)\Spark\Spark.exe" "username=%USERNAME%&server=example.org"
and so from the batch file, the following works:
start "" "C:\Program Files (x86)\Spark\Spark.exe" "username=%USERNAME%&server=example.org"
echo Spark started
Much thanks and points to dcp for getting me to the right answer.
Did you try this?
"C:\Program Files (x86)\Spark\Spark.exe" "\"user=foo&server=example.org\""
This worked when I tried a simple command line test and c++ program (I could see the quotes when I printed the argv[1] argument).
UPDATE:
If %USERNAME% has spaces in it, then you need to quote it like this (see below). I think you can remove the other quotes.
"C:\Program Files (x86)\Spark\Spark.exe" user="%USERNAME%"&server=example.org

Using the "start" command with parameters passed to the started program

I have a Virtual Machine in Virtual PC 2007.
To start it from the desktop, I have the following command in a batch file:
"c:\program files\Microsoft Virtual PC\Virtual PC.exe" -pc "MY-PC" -launch
But that leaves a dos prompt on the host machine until the virtual machine shuts down, and I exit out of the Virtual PC console. That's annoying.
So I changed my command to use the START command, instead:
start "c:\program files\Microsoft Virtual PC\Virtual PC.exe" -pc MY-PC -launch
But it chokes on the parameters passed into Virtual PC.
START /? indicates that parameters do indeed go in that location. Has anyone used START to launch a program with multiple command-line arguments?
START has a peculiarity involving double quotes around the first parameter. If the first parameter has double quotes it uses that as the optional TITLE for the new window.
I believe what you want is:
start "" "c:\program files\Microsoft Virtual PC\Virtual PC.exe" -pc MY-PC -launch
In other words, give it an empty title before the name of the program to fake it out.
Instead of a batch file, you can create a shortcut on the desktop.
Set the target to:
"c:\program files\Microsoft Virtual PC\Virtual PC.exe" -pc "MY-PC" -launch
and you're all set. Since you're not starting up a command prompt to launch it, there will be no DOS Box.
You can use quotes by using the [/D"Path"] use /D only for specifying the path and not the path+program. It appears that all code on the same line that follows goes back to normal meaning you don't need to separate path and file.
start /D "C:\Program Files\Internet Explorer\" IEXPLORE.EXE
or:
start /D "TITLE" "C:\Program Files\Internet Explorer\" IEXPLORE.EXE
will start IE with default web page.
start /D "TITLE" "C:\Program Files\Internet Explorer\" IEXPLORE.EXE www.bing.com
starts with Bing, but does not reset your home page.
/D stands for "directory" and using quotes is OK!
WRONG EXAMPLE:
start /D "TITLE" "C:\Program Files\Internet Explorer\IEXPLORE.EXE"
gives:
ERROR "The current directory is invalid."
/D must only be followed by a directory path. Then space and the batchfile or program you wish to start/run
Tested and works under XP but windows Vista/7/8 may need some adjustments to UAC.
-Mrbios
The spaces are DOSs/CMDs Problems so you should go to the Path via:
cd "c:\program files\Microsoft Virtual PC"
and then simply start VPC via:
start Virtual~1.exe -pc MY-PC -launch
~1 means the first exe with "Virtual" at the beginning. So if there is a "Virtual PC.exe" and a "Virtual PC1.exe" the first would be the Virtual~1.exe and the second Virtual~2.exe and so on.
Or use a VNC-Client like VirtualBox.
None of these answers worked for me.
Instead, I had to use the Call command:
Call "\\Path To Program\Program.exe" <parameters>
I'm not sure this actually waits for completion... the C++ Redistributable I was installing went fast enough that it didn't matter
If you want passing parameter and your .exe file in test folder of c: drive
start "parameter" "C:\test\test1.exe" -pc My Name-PC -launch
If you won't want passing parameter and your .exe file in test folder of c: drive
start "" "C:\test\test1.exe" -pc My Name-PC -launch
If you won't want passing parameter and your .exe file in test folder of H: (Any Other)drive
start "" "H:\test\test1.exe" -pc My Name-PC -launch
The answer in "peculiarity" is correct and directly answers the question. As TimF answered, since the first parameter is in quotes, it is treated as a window title.
Also note that the Virtual PC options are being treated as options to the 'start' command itself, and are not valid for 'start'. This is true for all versions of Windows that have the 'start' command.
This problem with 'start' treating the quoted parameter as a title is even more annoying that just the posted problem. If you run this:
start "some valid command with spaces"
You get a new command prompt window, with the obvious result for a window title.
Even more annoying, this new window doesn't inherit customized font, colors or window size, it's just the default for cmd.exe.
If you must use double quotation mark at any parameter, you can get error "'c:\somepath' is not recognized a an internal or external command, operable program or batch file".
I suggest below solution when using double qoutation mark:
https://stackoverflow.com/a/43467194/3835640
/b parameter
start /b "" "c:\program files\Microsoft Virtual PC\Virtual PC.exe" -pc "MY-PC" -launch
have you tried:
start "c:\program files\Microsoft Virtual PC\Virtual PC.exe" "-pc MY-PC -launch"
?
Put the command inside a batch file, and call that with the parameters.
Also, did you try this yet? (Move end quote to encapsulate parameters)
start "c:\program files\Microsoft Virtual PC\Virtual PC.exe -pc MY-PC -launch"
Change The "Virtual PC.exe" to a name without space like "VirtualPC.exe" in the folder.
When you write start "path" with "" the CMD starts a new cmd window with the path as the title.
Change the name to a name without space,write this on Notepad and after this save like Name.cmd or Name.bat:
CD\
CD Program Files
CD Microsoft Virtual PC
start VirtualPC.exe
timeout 2
exit
This command will redirect the CMD to the folder,start the VirualPC.exe,wait 2 seconds and exit.

How to create batch file in Windows using "start" with a path and command with spaces

I need to create a batch file which starts multiple console applications in a Windows .cmd file. This can be done using the start command.
However, the command has a path in it. I also need to pass paramaters which have spaces as well. How to do this?
E.g. batch file
start "c:\path with spaces\app.exe" param1 "param with spaces"
Actually, his example won't work (although at first I thought that it would, too). Based on the help for the Start command, the first parameter is the name of the newly created Command Prompt window, and the second and third should be the path to the application and its parameters, respectively. If you add another "" before path to the app, it should work (at least it did for me). Use something like this:
start "" "c:\path with spaces\app.exe" param1 "param with spaces"
You can change the first argument to be whatever you want the title of the new command prompt to be. If it's a Windows app that is created, then the command prompt won't be displayed, and the title won't matter.
Escaping the path with apostrophes is correct, but the start command takes a parameter containing the title of the new window. This parameter is detected by the surrounding apostrophes, so your application is not executed.
Try something like this:
start "Dummy Title" "c:\path with spaces\app.exe" param1 "param with spaces"
start "" "c:\path with spaces\app.exe" "C:\path parameter\param.exe"
When I used above suggestion, I've got:
'c:\path' is not recognized a an internal or external command, operable program or batch file.
I think second qoutation mark prevent command to run. After some search below solution save my day:
start "" CALL "c:\path with spaces\app.exe" "C:\path parameter\param.exe"
Interestingly, it seems that in Windows Embedded Compact 7, you cannot specify a title string. The first parameter has to be the command or program.
You are to use something like this:
start /d C:\Windows\System32\calc.exe
start /d "C:\Program Files\Mozilla
Firefox" firefox.exe start /d
"C:\Program Files\Microsoft
Office\Office12" EXCEL.EXE
Also I advice you to use special batch files editor - Dr.Batcher
Surrounding the path and the argument with spaces inside quotes as in your example should do. The command may need to handle the quotes when the parameters are passed to it, but it usually is not a big deal.
I researched successfully and it is working fine for me. My requirement is to sent an email using vbscript which needs to be call from a batch file in windows. Here is the exact command I am using with no errors.
START C:\Windows\System32\cscript.exe "C:\Documents and Settings\akapoor\Desktop\Mail.vbs"

Resources