Click to See Complete Forum and Search --> : WSH


Kyle Burns
February 22nd, 2000, 01:49 PM
I posted earlier asking about a script editor, then thought maybe I would write a routine to convert a .bas file to .vbs. I've written a routine to do this and would like to submit to see if anyone can see changes that would make this routine work better. I would appreciate any suggestions or comments and will graciously ignore any snide remarks :)

option Explicit
'bas2vbs.vbs
'converts bas file to vbs file for use
'with WSH
'NOTE: Still need to be careful to only use
'control structures and functions available in
'VBScript in your BAS


Dim oFS
Dim oFileIn
Dim oFileOut

Dim sFilePathIn
Dim sFilePathOut

Dim sLineText

sFilePathIn = InputBox("Enter Name of File to convert")

sFilePathOut = Left(sFilePathIn, Instr(sFilePathIn, ".")) & "vbs"

set oFS = WScript.CreateObject("Scripting.FileSystemObject")

set oFileIn = oFS.OpenTextFile(sFilePathIn)
set oFileOut = oFS.CreateTextFile(sFilePathOut)

Do Until oFileIn.AtEndOfStream = true
sLineText = oFileIn.ReadLine()
If UCase(Left(sLineText, 9)) = "ATTRIBUTE" then sLineText = ""
Dim iPos
iPos = InStr(UCase(sLineText), " as ")
If iPos > 0 then
sLineText = Trim(Left(sLineText, iPos - 1))
End If
If Instr(UCase(sLineText), " CREATEOBJECT(") > 0 then
Dim sTemp
Dim arText
sTemp = ""
arText = Split(sLineText, " ")
Dim iCur
for iCur = LBound(arText) to UBound(arText)
If Left(UCase(arText(iCur)),12) = "CREATEOBJECT" then
sTemp = sTemp & "WScript." & arText(iCur)
else
sTemp = sTemp & arText(iCur)
End If
sTemp = sTemp & " "
next
sLineText = Trim(sTemp)
End If
If sLineText > "" then oFileOut.WriteLine sLineText
Loop

oFileIn.Close
oFileOut.Close

set oFileIn = nothing
set oFileOut = nothing
set oFS = nothing

Lothar Haensler
February 23rd, 2000, 02:42 AM
my very personal comments:
- I'd rather use PERL to do the conversion; it' ll probably take only 5 % of the required lines of code.
- you should take comment lines into account
- AFAIK VBSCript does not support Declare statements, as well as Public and so on.
You might wanna check for these (and issue warnings?)

First of all, why convert BASs to VBS, when you can Createobject?
I'd rather leave my VB COM objects as they are and use them from WSH.

Kyle Burns
February 23rd, 2000, 06:44 AM
Thanks for your comments. You're right about the declare statements, and I'll have to double check on the Public thing. The reason I want to convert BASs to VBS is that I have to write VBS scripts for a lot of administrative tasks where I cannot send libraries that need registered on the target machine. I'm using WSH as an advanced replacement for BAT files. Unfortunately, VB has made me lazy over the years and I would much rather have things like auto-complete than keep an object model handy to double check what methods and properties I have available to me. As for the PERL thing, I never learned it. Is it difficult to learn?

Lothar Haensler
February 23rd, 2000, 06:47 AM
PERL is one of the most widely used scripting language on the Web as far as I know.
Is it easy? kind of.
But, it offers so many different ways to solve problems that you could say:
it's fairly easy to learn, but it can be extremely hard to read (and understand) someone else's scripts.

Lothar Haensler
February 23rd, 2000, 06:48 AM
one more idea: wouldn't it be cool to have a VB Add-in that would convert the current module into a VBS file?

Kyle Burns
February 23rd, 2000, 06:52 AM
That's a great idea!

Atlantisoft
February 23rd, 2000, 06:55 AM
i'm sure this is a blatently obviose question to all you pros, but is it possible to MAKE vb add-ins?

Lothar Haensler
February 23rd, 2000, 06:58 AM
>but is it possible to MAKE vb add-ins?

it sure is. I have written a couple of them.
VB 6 comes even with a project type of Add-in that you can select, when you choose File/New Project...

Chris Eastwood
February 23rd, 2000, 07:30 AM
PERL is great for handling text processing / formatting - like Lothar says, it's quite easy to learn, but some programmers (probably those with a 'C' Background) like to cram as many statements onto one line as possible, making it a nightmare to read some programs.

BTW, I seem to remember that you can get PERL as a 'plugin' language for the WSH - anyone else heard anything about this ?


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Lothar Haensler
February 23rd, 2000, 07:34 AM
WSH is - as its name implies - a scripting host.
Thus you can "plug in" any Activex Scripting language, such as Pythonscript and YES, PERLScript.
This is about the same technology that you can use in IE scripting. IE can also "run" Perlscripts (IE does, of course not run it, but rather host the scripting engine).

Lothar Haensler
February 23rd, 2000, 07:39 AM
>some programmers (probably those with a 'C' Background) like to cram as many statements onto one line as possible, making it a nightmare to read some programs

there is a philosophy in our department (more like a joke): if your PERL script doesn't fit onto one line, you haven't really exploited the full functionality of the language :-)

Kyle Burns
February 23rd, 2000, 08:16 AM
Caught a major oversight on this. In removing the type in my variable declarations, I killed functions.

Function MyFunc(Expression as string) as string



Would be hosed beyond belief

Function MyFunc(Expression

Lothar Haensler
February 23rd, 2000, 10:10 AM
BTW, here is a PERL script that does what your VBScript does:
s/(Dim |Public |Private )(\w+) As.*/Dim \2/i;
s/(Private|Public) (Sub|Function .*)/\2/i;
s/CreateObject\("(.*)"\)/WScript.CreateObject("\1"/gi;
if( /^Attribute.*/ ) {} else { print; }

...it even takes care of Private and Public declares...

still far from being perfect, but it might give you an idea of the power of PERL.