Call Ruby from a Vbscript

Call Ruby from a VBScript

You need a shell (%comspec% /c) to get a shell's feature like > redirection. So change

obj = newobj.Run("ruby E:\rubyfile.rb > D:\newdoc.txt",1,true)

to

nRet = newobj.Run("%comspec% /c ruby E:\rubyfile.rb > D:\newdoc.txt",1,true)

(Study the docs for .Run to see the reason for nRet instead of obj and spend a thought on the lousy-ness of the name "newobj")

Making calls and passing values to a vbscript from a ruby script

Try calling the Windows Scripting Host (cscript) with your script:

 system "cscript //nologo script.vbs #{random_filename}"

Is it practical to call vbscript functions from Ruby using win32ole?

I wouldn't call a vbscript script for something that is easier done in Ruby but it is possible.

How you requested it:

require 'win32ole'

sc = WIN32OLE.new("ScriptControl")
sc.language="VBScript"
center_header = sc.eval('"&F" & Chr(10) & "&A"') #"&F\n&A"

and how you could do it in Ruby itself

"&F" + 10.chr + "&A" #"&F\n&A"

or

"&F\n&A" #"&F\n&A"

EDIT: For 64bit windows that wouldn't work any longer out of the box, you need to install a 64 bit dll that you can find at https://tablacus.github.io/scriptcontrol.html (The site is in japanese so translate the page in your browser)

VBscript equivalent of a simple ruby script

Looks like you're 90% of the way there.

Since VBScript doesn't provide direct random access to string contents, you need to use some functions to do the string manipulation.

I'd go with Mid and Left.

E.g.

dim a, b
a = "Hello, World!"
b = left( mid( a, 8 ), 5 )
wscript.echo(b)

Output: World

using a vbscript to run another vbscript in all subfolders?

Got you this from my library of snippets, adjust to you needs.
Not tested, I'm more in to Ruby the last couple of years.

'call our sub with the desired path as option
' this needs to be a Folder object, not a String
' FSO.GetFolder will return that object given a path as string
ShowSubfolders FSO.GetFolder(path)

Sub CreateHtml(path)
' put here the code from your other script (best)
' or do your call to execute the other script
' you probably need the path so you pass it as a parameter
End Sub

Sub ShowSubFolders(Folder)
' a Folder has the method SubFolders,
' gives a collection of subfolders that can be enumerated
' with For Each construct
For Each Subfolder in Folder.SubFolders
' print the subfolder so you can follow the progress
Wscript.Echo Subfolder.Path
' and call the sub that creates the html file
CreateHtml Subfolder.Path
' here the magic of recursion, the sub is calling itself with
' as parameter the subfolder to process the subsubfolders etc
ShowSubFolders Subfolder
Next
End Sub

NB in Ruby it's just one line Dir["#{folder}/*"].each{|f| puts f if File.directory?(f) }

Run a command and capture the output in vbscript

  1. dir is intrinsic; you need %comspec%.
  2. Double quotes need to be escaped by double double quotes in VBScript:

    Wscript.Echo runCMD("%comspec% /c dir /A-d ""C:\Windows\Minidump"" | find /c ""/""")

How to run a file using VisualBasicScript (.vbs)

yes i want to run it.

Then try this:

CreateObject("WScript.Shell").Run "file.bat"


Related Topics



Leave a reply



Submit