Scenario :
I mapped a network drive on a Win XP machine and I double click a .bat file to execute this Ruby script. The .rb and .bat file reside on this networked drive.
The batch file is as follows :
Z:
cd Z:\ABC\StatusCheck\
"C:\Program Files\Ruby\Bin\ruby.exe" Z:\ABC\StatusCheck\rubyScript.rb 6
The Ruby file is as follows :
require 'watir'
rec = File.open("list.txt", "r")
ie = Watir::IE.start()
***Other processing here***
My Question : How do I instantiate this batch file using Linux (when I am at home cos I cannot remote into to this machine. I want to run the .rb file from the terminal)?
Hope I made sense. I really appreciate your time guys! Thank you!
You don't need any batch file to run this on linux. All you need to do is run the script directly with
ruby rubyScript.rb
or add
#!/usr/bin/env ruby
to the top of rubyScript.rb and make it executable, then you can run directly.
However, your bigger problem is that you are using watir to automate IE, which obviously won't work on Linux, so you'll need to change it to use another browser.
Related
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?".
I am doing prep work for App Academy but I am having a hard time setting up files/directories to be able to run everything correctly. I have a couple questions and haven't been able to find concrete answers:
How do you make a directory in the windows terminal?
How do you run files previously written in Notepad in IRB? I wrote some simple scripts in Notepad because Sublime was causing me severe migraines. Or how do you change Notepad files to Ruby files?
How do you create new files in IRB like test_code.rb?
1) When at the Windows command prompt and having navigated to your working directory, type md directory_name
2) Unless you have specifically told notepad otherwise, notepad will have saved your file as a .txt file. You will simply have to rename the file extension from *.txt to *.rb.
3) When you installed Ruby on your Windows machine, the Ruby interpreter would have undoubtably been added to your path, so you should just be able to run your *.rb file direct from the windows cmd prompt and it will execute.
To load it while in IRB: Make sure you run IRB from the same folder as your *.rb file is in. Once you have cranked up an IRB session, type load 'my_file.rb'.
IRB is a great environment for testing code, but not for writing full scripts. Use Notepad or Notepad++ or Vim for Windows or your editor of choice, as long as it's capable of generating a text (non word processing document).
You can make a directory in the terminal or in the Explorer, it doesn't matter. Just note where you created it so you don't lose it.
If you want to run a script in Ruby, simply type ruby /path/to/the/file/script_to_run.rb and the Ruby interpreter should load and run the file.
You can load a script into IRB and watch it run, but that's rarely something we need to do. More often you'll want to run scripts using Ruby, and try things in IRB, since it's like a scratchpad.
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
This is a noob question at its best but Google isn't finding what I need.
I have a ruby script that I need to fire off via task scheduler. I thought I did it right by using the following:
cmd /k ruby test.rb
This works when starting the .bat file from the folder. However, if it runs from taskeng.exe it fails because its looking in my system32 folder. I don't want it to run from system32 so how do i format this to run from say, c:/dev/
Again, sorry for the extremely noob question.
You can leave out cmd of that and just use
ruby test.rb
or rather (in your case):
ruby C:\Users\Foo\test.rb
or something like that. Giving the complete path to the script usually helps in finding it ;-)
In any case, if you need the working directory you can set it in the scheduled task itself. Something akin to the following:
Likewise, if you actually need cmd in there. Just fill out the Start in field and you have a working directory.
I am trying to run the program nbtstat.exe(located in c:\windows\system32) from a ruby script. The problem is that it appears that Windows 7(64-bit) is hiding the program from the ruby script(it works fine in Vista).
For example,
Nbtstat command: "nbtstat"
Dir command: dir "C:\Windows\System32\n*.exe"
If I run the nbtstat command in just the command line, nbtstat will run fine. If I then run the dir command, nbtstat.exe will show up in the list of files in that directory.
However, if I run the nbtstat command from the ruby script(using backticks, system, %x or Kernel.open), it will not succeed. If I then run the dir command(also from the ruby script), it will show a list of files in the directory minus nbtstat.exe and a few others.
UAC is turned off and it is being ran from an administrator. I tried this in both ruby 1.8.6 and 1.9.2.
I created a .bat file that runs the nbtstat command and it worked fine but if I call the .bat file from the ruby script it will fail.
Any ideas?
Thank you.
On Win64, if you run a 32-bit process, Windows will remap the c:\windows\system32 directory to actually point to c:\windows\syswow64. That directory doesn't contain the binary you're looking for, so your 32-bit process doesn't see it.
You can detect whether that's the case by looking at the process's environment (look at the output of "set" for the env variables), although I don't know of a way to make the process see the actual 64-bit directory.
Maybe the shell in which your ruby script is launched was created before the PATH is initialized. I noticed this issue in Windows 7. You can try to print PATH in your script to verify if you are encountering this issue.