CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: WSH

  1. #1
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    WSH

    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





  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    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.


  3. #3
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: WSH

    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?


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    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.



  5. #5
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    one more idea: wouldn't it be cool to have a VB Add-in that would convert the current module into a VBS file?


  6. #6
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: WSH

    That's a great idea!


  7. #7
    Join Date
    Feb 2000
    Location
    America
    Posts
    130

    Re: WSH

    i'm sure this is a blatently obviose question to all you pros, but is it possible to MAKE vb add-ins?


  8. #8
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    >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...


  9. #9
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: WSH

    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

  10. #10
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    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).


  11. #11
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    >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 :-)


  12. #12
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: WSH

    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





  13. #13
    Join Date
    May 1999
    Posts
    3,332

    Re: WSH

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured