How to automate Windows Run application using Ruby - ruby

I need to open Run from my Ruby script and type the location of a file and click OK. I have seen some examples to open notepad and entering text using WIN32OLE but I am not sure how to open the Run command.

If you are using Windows, I think you can do:
`start location_of_my_file`

You can do that with any of the following commands in ruby
1) exec
2) using backtick or %x
3) system
Along with the name of the file you should also give the name of the program that should execute it.
Ex: If you want to open calculator then you can just do
exec 'calc' # or `calc` or %x(calc) or system 'calc'
Ex: If you want to open a text file in notepad then :
exec 'notepad file_name.txt'
or
`notepad file_name.txt`
or
%x(notepad file_name.txt)
or
system 'notepad file_name.txt'

Here is one way that you can do it:
require 'win32ole'
def power
wsh = WIN32OLE.new('Wscript.Shell')
if not wsh.AppActivate('powershell')
wsh.Run('powershell')
sleep(3)
wsh.SendKeys('gwmi win32_bios{ENTER}')
wsh.SendKeys('gwmi win32_processor{ENTER}')
wsh.SendKeys('gwmi win32_volume{ENTER}')
wsh.SendKeys('ls{ENTER}')
wsh.SendKeys('ping 192.168.0.14{ENTER}')
wsh.SendKeys('exit')
end
end
power

Related

Running keystrokes from program

I'm writing a program to automate an e-mail process and would like to know if there's a way to run keystrokes from within the program?
For example say I have this string:
str = "test"
And it gets copied to a file:
File.open('str.txt', 'w') { |s| s.puts(str }
And after that I want to use CNTRL-A; CNTRL-C on the file and copy the information, is this possible in a Ruby program, without the use of external gems?
Operating system: Windows 7
If sending arbitrary keystrokes to other applications is what you're after you can use the gem https://github.com/erinata/auto_click for it. However, if you can't use gems, what you can do instead is run NirCmd (or one of its alternatives) with the appropriate command line arguments to achieve the same result.
For example:
# Tell the OS to bring up the Notepad window and give it the time
# to do so.
`nircmd win activate ititle notepad`
sleep 0.5
# Select all text in the Notepad window and copy it to the
# clipboard.
`nircmd sendkeypress ctrl+a`
`nircmd sendkeypress ctrl+c`
If you can't install gems but can copy ClipText.exe to your current directory, do so then run this code in Ruby:
File.open('str.txt', 'w') { |s| s.puts(str }
`cliptext.exe from str.txt`
For a more rigorous way of executing commands on Windows see "How to execute Windows CLI commands in Ruby?".

Ruby -- Get User Input outside of the Command Line using Ocra

So I have a very simple single file script:
puts "Enter the file name"
file = gets.chomp
puts "What do you want to replace it with?"
replace = gets.chomp
which then changes some files with the user-inputs. I packaged it up with Ocra, but I was hoping it would open up the command line when it was run and ask for the user inputs or something, or a pop-up window maybe. I need this to be very simple since my users won't know to go to the command line and run the .exe from there with arguments, so is there a way to to get a window to pop-up that takes in user input every time the .exe file is run? I've tried it in both .rb and .rbw formats.
cmd (or maybe it's cmd.exe) should work on Windows. It shells out to the cmd command which should launch a cmd window.

Running cmd scripts from a ruby file?

Is there anyway to write commands to the command prompt in windows and execute directly from a ruby program?
I would use this as a one click installer for all the gems I wanted to install on the computer after installing ruby. I hope that it would save time when transferring my ruby files to a new computer. Or would be an easier way to get a non-ruby person set up very quickly with all the gems I thought them might need.
I am imagining something like Watir but for the Cmd rather than a browser.
EDIT
Thanks to
How can I then close the cmd window without closing the program for instance:
'notepad'
starts a cmd window and it also starts notepad but the cmd windows stays until the notepad is closed.
Ruby will execute anything you put in backticks ` in your associated shell.
so if you type
test = `ipconfig`
puts test
test should now have stored in it the data from the cmd call ipconfig
EDIT
You can also use the System(..) call in Ruby to execute commands

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

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