How to open a command prompt window in ruby script and have an interactive session - ruby

I have a requirement where i need to open command prompt and run a bat file, this bat file will ask for the user to enter a choice 2 to 3 times, how to automate this in ruby in windows.
Am able to open command prompt using
system("start cmd.exe")
After this I need to change directory and then i need to run the file present in c://temp//dat.bat, through ruby script.
Please let me know how to automate all these operations.

cmd_line = "cmd.exe /c \"cd #{directory}&&#{bat_file}\""
system(cmd_line) # if you're not interested in the output
Should do it.

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,

Open another shell from cmd and execute commands from batch file

I'm creating a batch file which will open windows command prompt and from there it will open another shell(windchill shell). There I have to execute a command which will do some operations.
#echo off
start cmd.exe /k "cd D:\PTC\Windchill\bin & windchill shell"
Above script is opening windchill shell but I couldn't find a way to execute commands on it. I do not have much experience in this. Looking for some help.
Thanks in advance

Advise Request: How to launch three .bat files at the same time?

I got three separate .bat files. Each has the following three commands respectively.
file1.bat: cmd /K "cd C:\PythonScripts\X3865\ & python run_all.py"
file1.bat: cmd /K "cd C:\PythonScripts\X3865_1\ & python run_all.py"
file1.bat: cmd /K "cd C:\PythonScripts\X3865_2\ & python run_all.py"
Currently to execute the program, I go to the Windows file folder and double click on each of them to launch the programs. They come up in three different command windows.
I want to combine the above commands in 1 single .bat file, which when executed will launch all the three programs automatically. How can I do this?
The answers from Mark and ian0411 will run the three python scripts one after the other. If that's what you want, go with either of those. If you want to run the three scripts simultaneously, your batch file should be
start cmd /c python "c:\pythonscripts\x3865\run_all.py"
start cmd /c python "c:\pythonscripts\x3865_1\run_all.py"
start cmd /c python "c:\pythonscripts\x3865_2\run_all.py"
This will start each in a separate window; if there is output that is needed from the command(s), change the /c to /k; this will leave the window open until you close it manually.
This should run the Python scripts in order:
python "C:\PythonScripts\X3865\run_all.py"
python "C:\PythonScripts\X3865_1\run_all.py"
python "C:\PythonScripts\X3865_2\run_all.py"
Their outputs will all be printed to the same cmd window. If you wish to see the outputs at the end, add pause to the end of the bat file so that it won't automatically close.
Save the following into a .bat file
python "C:\PythonScripts\X3865\run_all.py"
python "C:\PythonScripts\X3865_1\run_all.py"
python "C:\PythonScripts\X3865_2\run_all.py"
And then just run that .bat file and it should do it.

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 close the command prompt when running a batch file in ruby in windows

am opening command prompt using system command like system("start cmd.exe") and then running some batch file. I want to stop the batch file running in command prompt or close the command prompt window. Could you please help me with this.
If you want the command prompt to terminate you just write the following in your script..:
system("exit")
To stop the batch file I will use win32ole lib and send a CTRL+C combo like this..:
require "win32ole"
key = WIN32OLE.new('Wscript.Shell')
key.SendKeys('{^}C{ENTER}')

Resources