How to execute a .lnk file in Ruby - ruby

I am a beginner in Ruby and I wanted to start an application in Ruby. But how do I do that, how do I open a .lnk file in Ruby?

You can open a file for reading with the File#open method:
File.open('file.ink') do |f|
# process the file
end
To write to the file pass w as the second argument to File#open.

Related

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.

Error when opening Ruby File (Beginner) + How to run a file?

When I try to open A ruby file in the same directory something weird happens,
like so :
C:\RubyFiles>file = File.open("Lottery.rb", "r")
'file' is not recognized as an internal or external command, operable program or batch file.
now I know this has nothing to do with the opening of the file itself, but I wanted to have an example ;)
(This has been resolved^) :D
But now I would like to know how to run the file itself? Can anyone help? Thanks in advance!
After opening irb, you can execute a ruby file using Kernel#load
load 'Lottery.rb'
If this file just includes module/class definitions, you probably only want to load it once. This is what Kernel#require ensures:
require 'Lottery.rb'

How to automate Windows Run application using 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

How do I automatically open the file I created?

I create a file when I run my Ruby script. How would I set the script to open the file with a default editor or a text editor once the script is finished?
For instance, I create a file called "FooBar.txt" that gets a bunch of information loaded into it. How can I open that up afterwards?
I am sure this is really simple, but every time I search for it all it comes up with is opening a file to add text or edit in the script. I'm not actually opening the file with a program.
You can make a system call.
This is an example with Windows:
filename = "the_list.txt"
File.open(filename, "w"){|file|
file << "Some data\n"
}
`call notepad #{filename}`
This calls Notepad with the given filename.
Some variants to call an external program are:
`notepad #{filename}`
system( "notepad #{filename}")
system( "call notepad #{filename}")
%x{call notepad #{filename}}
You even don't need to add notepad:
%x{call #{filename}}
This depend on the main application, which is assigned to the extension of the file you create.
When you tell which system and which editor you need, more details are possible.
Another possibility:
require 'open3'
Open3.popen3("call #{filename}")
#or:
#Open3.popen3("call notepad #{filename}")
The advantage is the main program does not wait until the subprocess ends.
Variant as script: Store the following code as "file_build.rb".
filename = ARGV.first
File.open(filename, "w"){|file|
file << "Some data\n"
}
require 'open3'
puts "Call Editor"
Open3.popen3("call notepad #{filename}")
puts "End of script"
Now you can call file_build.rb test.txt. test.txt is created, an editor is called and the script closes. The editor keeps running, at least it did in my test (WinXP).

In ruby am unable to open a text file from a specified location, where as am able to open a file which I have created on my own

I have a file which is placed in C:\mtn-2\mtn-2.2\logs\messages.txt, if am trying to open this file via script it says no such file or directory, so I have created another file called a.txt in the same location and tried to open via ruby script its working file.
messages.txt file am able to rename/modify manually but the same it not working through the script.
Am working on windows xp.
The code which I have written is :
f=File.open("C://mtn-2//mtn-2.2//logs//messages.txt", "r") // not working, this is created by some tool,
f=File.open("C://mtn-2//mtn-2.2//logs//a.txt", "r") // its woking, this is created by me.
Waiting for your early reply
The messages.txt file should be closed before it is opened by your program for reading.

Resources