Using Batch File to Open Another Cmd Prompt and Run Cmd in that Cmd Prompt - visual-studio-2010

I have another bat file that I'm running, and once in the command prompt that bat file creates, I want to run another command in that window.
Here's what I have so far:
call C:\Batch\MyBatFile.bat (this creates the new command prompt that I want to use)
C:\Program\MyProgram.exe
However, the second line is being run in the original window, instead of the new command prompt. I tried using start C:\Program\MyProgram.exe, but that just ran in a 3rd new window instead.
If it's relevant, the first line is just setting a few environment variables that I need access to and MyProgram is a visual studio 2010 project. Technically, I might be able to modify that bat to run the command, but I'd rather avoid that solution as that bat file isn't owned by me (and thus whenever it's updated I'd have to update mine as well).
Thanks in advance.

You could try to inject your program.exe into cmd created by batfile.bat by redirecting it's input stream and then sending it a command, eg. echo C:\Program\MyProgram.exe | C:\Batch\MyBatFile.bat. This assumes that batch really just sets bunch of variables and does not use commands which reset/consume input stream.
Please note that if redirected/piped this way new command window will not stay open It will maybe :-) just execute your command and then close/exit.

Create a CMD script to run both of the commands that you have shown in the question. Maybe call it RunMyProgram.cmd. The contents are just the two lines that you have:
REM Source the environment variables.
REM Any new command prompt window that is opened can be ignored
CALL C:\Batch\MyBatFile.bat
C:\Program\MyProgram.exe
If what you have stated in the comments to your question is accurate regarding MyBatFile.bat setting up the environment variables and then starting a new window, then you should be able to make use of those environment variables after MyBatFile.bat exits.
If running RunMyProgram.cmd from a command prompt still has MyProgram.exe giving the error when the environment variable is not set, or if MyProgram.exe doesn't even start to run until you close the new window that popped up, then we need to see the exact commands that MyBatFile.bat is executing.

Related

Is there a command in Shell scripting for executing .exe file and running commands automatically inside of it? replacing the user interaction

I have a .sh script file that I'm modifying which runs an .EXE file that opens the Windows command line prompt automatically.
This .exe asks the user for an input (name of the file in the folder workspace that it will read)
I want to automate this step in my shell script so my user doesn't have to interact with this, and run the commands automatically
I read a bit about the expect command but I think that is for Linux only.
Can someone help me, I'm pretty new to Shell scripting and I couldn't find any useful information elsewhere.
I'm assuming that your executable accepts command-line arguments. So, here we go.
You can use the "start" command in Windows Shell. For example:
start C:\path\to\program.exe -argument
If you want to make the script wait until the .exe file finishes running before continuing, you can use the "/wait" command:
start /wait C:\path\to\program.exe -argument
IF all of that doesn't work, please try:
start myprogram.exe /command1 /command2 /command3
Hope it helps,

Cygwin: launch bat with parameter, in new window, with explorer environment

I need to run some .bat files from Cygwin. Until now, these bat files didn't required any param, so I used the following method:
chmod +x $strBatFilename
cygstart "$WINDIR/explorer.exe" "$strBatFilename"
These commands implement 2 features that I need:
The .bat file is opened in a new window
The environment of the new window is the explorer's one, not the one of Cygwin
The problem that I have is that now I need to also pass a parameter to the bat file.
I don't know of a method to call explorer.exe with a file (my bat) as parameter and having some extra option to pass another parameter to the bat (sounds like passing a parameter to a parameter of explorer.exe).
I have searched and found that I can start a cmd.exe. I tried starting it directly or starting it using cygstart:
cygstart $WINDIR/system32/cmd.exe /c start "$strBatFilename $strBatParam"
I have managed to make it start in a new window and launch the .bat with a parameter, but the environment is not the explorer's one.
I don't know the exact differences between the two environments. What makes me say they are different is that when for example the bat contains a repo sync command, if it's open with explorer it runs the repo sync command smoothly, while if the bat is called from cmd.exe, the repo sync command will ask for my user name and email.
So, my question is: are you aware of any command that will start from Cygwin a .bat file
passing a parameter to it
opening it in a new window
having the new window inherit the explorer's environment ?
Thank you

batch file commands in powershell execute in a different command prompt

I'm using this new machine, so as usual I go and set the execution policy so that I can use my profile script, after doing that however powershell now opens all batch files in a new cmd.exe window.
I tried undoing this step but it's still the same so I think it has nothing to do with the script execution policy, also I still have the powershell window in which I originally set the execution policy and this one behaves normally, only new windows have this problem.
I may have installed some software, but nothing is related to windows, and I tried setting the PATH variable to its exact value in the working window but it does not work.
Batch files will open in a new window if the PATHEXT environment variable does not contain '.BAT' as one of the executable extensions.
To check the variable, enter the following at the PowerShell prompt: $env:PATHEXT

How do I make a cmd script that once it is run, remains open and accepts new commands?

I want to make a cmd script that performs an action but then remains open and I can type new commands.
I have failed to find the proper terms to google as my knowledge of shell is almost zero.
thank you!
You can specify a command shell to run a specific command during start-up using the /k switch. E.g.
cmd /k C:\InitialScript.bat
The command shell would execute the C:\InitialScript.bat batch file and remain open for the user to type further commands.
If you want to create an icon for users to use then create a shortcut and use the following as the target:
%WINDIR%\System32\cmd.exe /K C:\InitialScript.bat
If you already have a command shell window open, then just use the following which will run the batch file in the context of the existing shell:
C:\InitialScript.bat

How to prevent batch file (.bat) from closing terminal when running commands?

On a Windows 7 machine if I run a PHPUnit Selenium command like this manually in the terminal:
phpunit --verbose --log-junit _selenium_tests\results\home.xml _selenium_tests\frontend\home.php
It spawns a browser and runs the test just fine. Then it outputs the following on the screen:
Time: 10 seconds, Memory: 3.50Mb
OK (1 test, 3 assertions)
And the terminal stays open.
Now if I copy and paste the exact command in an empty file and save it as test.bat and click it, it also runs the test. I can see the browser open and all tests run. Only problem is it closes the terminal prompt right after. So I can't see the above output.
An even bigger problem is, since it closes the terminal if I add more commands for other tests after that initial one they don't run.
I tried adding:
pause
at the end of the bat file but no luck, it still closes. Any idea how to prevent this and be able to run one command after another without the terminal ever closing?
Your question is similar to this one. Try using call in front of your command. If you run a .bat file from another .bat file and don't use call, control doesn't return to the first batch file, so pause doesn't get executed.
Try cmd /K phpunit --verbose --log-junit _selenium_tests\results\home.xml _selenium_tests\frontend\home.php
The /K option in cmd /K string Carries out the command specified by string but remains,see http://www.computerhope.com/cmd.htm
Also, I don't know the file type of the phpunit command you execute - I'm not familiar with selenium. If it is batch file (i.e. ends with .bat), you just can't call them from another batch file: everything below the call to the second batch file will never get executed.
You then need to use the CALL command. CALL Enables a user to execute a batch file from within another batch file, see http://www.computerhope.com/call.htm

Resources