How to close the command prompt when running a batch file in ruby in windows - ruby

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}')

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,

How can I keep using the command prompt after executing a .cmd file in Windows?

After I double clicked a .cmd file and it executed successfully, it just closed the command prompt window. Even if I add pause to the end it also closes after I hit any key. So is there a way to let me keep using the command prompt, just as if I got the window from running cmd?
very simple.
Add the following line to the end of your code:
start /b cmd

Continue With CMD Commands After Batch Script

I am trying to write a batch script which, once complete, would allow the user to continue using the Windows command prompt as they normally would had no script been run. Is this possible? Thank you in advance for any help.
If you manually open CMD (the Command Prompt) and invoke the batch file by name, CMD will remain open for additional commands after the batch file completes. You cannot do this by double-clicking on the batch file, but if you create a shortcut to the batch file that runs CMD.EXE with the /K switch, you will run the batch file and then leave CMD running for additional commands. See CMD at SS64.

What is a batch file command to open a cmd window?

Yes, I tried googling this, by the way.
Basically, what is the dos command that will open a dos command window?
In other words, if you have a .bat file full of script commands, what command can you do that will open a command window?
You can try this maybe. In the command line it starts it so I assume in a bat file it'll do the same.
start cmd
If you want it to run another bat or something you can do:
start script.bat
If you want it to close then:
start cmd /c script.bat

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

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.

Resources