1) Open website in chrome using ruby script in windows
`start http://www.google.co.in`
------------------------------------------------------------------------------
2) creating directory
directory_name = "sree"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
------------------------------------------------------------------------------
3) open cmd using script
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('cmd.exe', '', '', 'open', 3)
____________________________________________________________________________
4) running cmd commands using ruby
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute("cmd.exe", "/c time 9:30:00 PM", "", "runas", 1)
-------------------------------------------------------------------------------------------------------------------------
5) Reading file
File.open('New Text Document.txt', 'r') do |f1|
while line = f1.gets
puts line
end
end
------------------------------------------------------------------------------------------------------------------------
6) open text file in cmd line
i) start sree.txt
ii) start c:\Users\mobigesture\Desktop\sree.txt
-------------------------------------------------------------------------------------------------------------------
7) opening txt file in browser using cmd line
@echo off
start chrome "c:\Users\mobigesture\Desktop\sree.txt"
_________________________________________________________________________
8) open app using cmd line
@echo off
start "Sticky Notes" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Sticky Notes"
--------------------------------------------------------------------------------------------------------------------------
http://ruby.bastardsbook.com/chapters/io/
9)
creating a file and writing to it-> save as file.rb --> open cmd--> run like -> ruby file.rb
fname="sample.txt"
f= File.open(fname,"w")
f.puts "hey this is test file"
f.close
(or)
f=File.open("sample.txt","w")
f.puts "hey welcome"
f.close
(or) ->>> if file is not exist it creates a new file
File.open("a.txt","w"){|f|
f.puts("apple !!")
}
--------------------------------------------------------------------------------------------------------------------------
http://ruby.bastardsbook.com/chapters/io/
10) Installing gem files in ruby
require 'rubygems'
require 'rest-client'
wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"
File.open(wiki_local_filename, "w") do |file|
file.write(RestClient.get(wiki_url))
end
--------------------------------------------------------------------------------------------------------------
11) creating , deleting, writing into file in cmd
i) creating file (-> dot is required after echo)
echo. >ruby.txt
ii) opening file
start ruby.txt
iii) deleting file
del ruby.txt
-------------------------------------------------------------------------------------------------------------------------
12) Reading from file (create file before running)
f=File.open("ruby.txt","r")
lines=f.read
puts lines
(or)
lines = File.open("ruby.txt", "r"){ |file| file.read }
puts lines
--------------------------------------------------------------------------------------------------------------
13) using Readlines & Readline
File.open("ruby.txt").readlines.each do |line|
puts line
end
(or)
file=File.open("ruby.txt","r")
while !file.eof
line=file.readline
puts line
end
---------------------------------------------------------------------------------
14)match line in array of strings
lines = ["Hello world", " How are you?", "*Fine*, thank you!.", " OK then."]
lines.each do |line|
puts line if line.match(/^ H/)
end
(or)
lines = ["Hello world","Hey", " How are you?", "*Fine*, thank you!.", " OK then."]
lines.each do |line|
puts line if line.match(/^H/)
end
o/p:
Hello world
Hey
--------------------------------------------------------------------------------------------------------------
15) closing files
file=File.open("ruby.txt","r")
lines=file.readlines
file.close
lines.each {|line| puts line}
(or)
lines=File.open("ruby.txt","r") { |file|
file.readlines
}
lines.each{ |line| puts line}
------------------------------------------------------------------------------------------------------------------
16) check if file exist or not---> checks in present directory
if File.file?("ruby.txt")
puts "yes"
else
puts "no"
end
(or)
if File.exist?("ruby.txt")
puts "yes"
else
puts "no"
end
-------------------------------------------------------------------------------------------------------
17)creating directory
========================================================================
Tutorials
1)
Create an instance of the Windows Shell object...
The shell object's ShellExecute method performs a specified operation on a specified file. The syntax is...
FILE: Required. String that contains the name of the file on which ShellExecute will perform the action specified by OPERATION.
ARGUMENTS: Optional. The parameter values for the operation.
DIRECTORY: Optional. The fully qualified path of the directory that contains the file specified by FILE. If this parameter is not specified, the current working directory is used.
OPERATION: Specifies the operation to be performed. It should be set to one of the verb strings that is supported by the file (Examples: 'open', 'edit', or 'print'). If this parameter is not specified, the default operation is performed.
SHOW: Recommends how the window that belongs to the application that performs the operation should be displayed initially (0 = hidden, 1 = normal, 2 = minimized, 3 = maximized). The application can ignore this recommendation. If this parameter is not specified, the application uses its default value.
So, to launch Excel in a maximized window...
I suppose you could also launch your rails app with something like this...
To print a document, hiding the application window...
That's about it. As always, post a comment here or send me email if you have questions, comments, or would like to request a topic for discussion.
Thanks for stopping by!
`start http://www.google.co.in`
------------------------------------------------------------------------------
2) creating directory
directory_name = "sree"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
------------------------------------------------------------------------------
3) open cmd using script
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('cmd.exe', '', '', 'open', 3)
____________________________________________________________________________
4) running cmd commands using ruby
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute("cmd.exe", "/c time 9:30:00 PM", "", "runas", 1)
-------------------------------------------------------------------------------------------------------------------------
5) Reading file
File.open('New Text Document.txt', 'r') do |f1|
while line = f1.gets
puts line
end
end
------------------------------------------------------------------------------------------------------------------------
6) open text file in cmd line
i) start sree.txt
ii) start c:\Users\mobigesture\Desktop\sree.txt
-------------------------------------------------------------------------------------------------------------------
7) opening txt file in browser using cmd line
@echo off
start chrome "c:\Users\mobigesture\Desktop\sree.txt"
_________________________________________________________________________
8) open app using cmd line
@echo off
start "__App_Name__" "__App_Path__.exe"
@echo off
start "Sticky Notes" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Sticky Notes"
--------------------------------------------------------------------------------------------------------------------------
http://ruby.bastardsbook.com/chapters/io/
9)
creating a file and writing to it-> save as file.rb --> open cmd--> run like -> ruby file.rb
fname="sample.txt"
f= File.open(fname,"w")
f.puts "hey this is test file"
f.close
(or)
f=File.open("sample.txt","w")
f.puts "hey welcome"
f.close
(or) ->>> if file is not exist it creates a new file
File.open("a.txt","w"){|f|
f.puts("apple !!")
}
--------------------------------------------------------------------------------------------------------------------------
http://ruby.bastardsbook.com/chapters/io/
10) Installing gem files in ruby
gem install rest-client
require 'rubygems'
require 'rest-client'
wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"
File.open(wiki_local_filename, "w") do |file|
file.write(RestClient.get(wiki_url))
end
--------------------------------------------------------------------------------------------------------------
11) creating , deleting, writing into file in cmd
i) creating file (-> dot is required after echo)
echo. >ruby.txt
ii) opening file
start ruby.txt
iii) deleting file
del ruby.txt
-------------------------------------------------------------------------------------------------------------------------
12) Reading from file (create file before running)
f=File.open("ruby.txt","r")
lines=f.read
puts lines
(or)
lines = File.open("ruby.txt", "r"){ |file| file.read }
puts lines
--------------------------------------------------------------------------------------------------------------
13) using Readlines & Readline
File.open("ruby.txt").readlines.each do |line|
puts line
end
(or)
file=File.open("ruby.txt","r")
while !file.eof
line=file.readline
puts line
end
---------------------------------------------------------------------------------
14)match line in array of strings
lines = ["Hello world", " How are you?", "*Fine*, thank you!.", " OK then."]
lines.each do |line|
puts line if line.match(/^ H/)
end
(or)
lines = ["Hello world","Hey", " How are you?", "*Fine*, thank you!.", " OK then."]
lines.each do |line|
puts line if line.match(/^H/)
end
o/p:
Hello world
Hey
--------------------------------------------------------------------------------------------------------------
15) closing files
file=File.open("ruby.txt","r")
lines=file.readlines
file.close
lines.each {|line| puts line}
(or)
lines=File.open("ruby.txt","r") { |file|
file.readlines
}
lines.each{ |line| puts line}
------------------------------------------------------------------------------------------------------------------
16) check if file exist or not---> checks in present directory
if File.file?("ruby.txt")
puts "yes"
else
puts "no"
end
(or)
if File.exist?("ruby.txt")
puts "yes"
else
puts "no"
end
-------------------------------------------------------------------------------------------------------
17)creating directory
dirname = "data-files"
Dir.mkdir(dirname) unless File.exists?dirname
========================================================================
Tutorials
1)
Launching Apps and Printing Docs with the Windows Shell
A reader recently asked how to launch an application from within a Ruby script. A quick answer is to use the system or execmethods. But you can also leverage the Windows Shell to launch applications, and have control over the window state. You can also use the shell to print documents. Let's get right down to it, shall we?...
Require the win32ole library...
Require the win32ole library...
require 'win32ole'
Create an instance of the Windows Shell object...
shell = WIN32OLE.new('Shell.Application')
The shell object's ShellExecute method performs a specified operation on a specified file. The syntax is...
shell.ShellExecute(FILE, ARGUMENTS, DIRECTORY, OPERATION, SHOW)
FILE: Required. String that contains the name of the file on which ShellExecute will perform the action specified by OPERATION.
ARGUMENTS: Optional. The parameter values for the operation.
DIRECTORY: Optional. The fully qualified path of the directory that contains the file specified by FILE. If this parameter is not specified, the current working directory is used.
OPERATION: Specifies the operation to be performed. It should be set to one of the verb strings that is supported by the file (Examples: 'open', 'edit', or 'print'). If this parameter is not specified, the default operation is performed.
SHOW: Recommends how the window that belongs to the application that performs the operation should be displayed initially (0 = hidden, 1 = normal, 2 = minimized, 3 = maximized). The application can ignore this recommendation. If this parameter is not specified, the application uses its default value.
So, to launch Excel in a maximized window...
shell.ShellExecute('excel.exe', '', '', 'open', 3)
I suppose you could also launch your rails app with something like this...
shell.ShellExecute('ruby.exe', 'c:\my_rails_app\script\server', '', 'open', 1)
To print a document, hiding the application window...
shell.ShellExecute('C:\MyFolder\Document.txt', '', '', 'print', 0)
That's about it. As always, post a comment here or send me email if you have questions, comments, or would like to request a topic for discussion.
Thanks for stopping by!