Script2EXE Home How to Use Interface Buy Support Site Home

Parse - A VBScript Sample


This sample shows how VBScript source originally written for CScript.exe can be adapted, so that the source can be converted to a Console program with Script2EXE.


Original Source for CScript.exe
'==========================================================================
' NAME: Parse.vbs
'
' AUTHOR: (c) Eskil Moe
' DATE  : 22.12.2005
' VER   : 1.0
'==========================================================================

If (WScript.Arguments.Count <> 2) And (WScript.Arguments.Count <> 3) Then 
    'X = x = MsgBox ("Usage: CScript.exe Parse.vbs <OldText> <NewText>  <ParseFile>"  & Chr(13) & "Usage: CScript.exe Parse.vbs <ReadVariablesFromFile> <ParseFile>", vbInformation, "Parse")
    X = x = MsgBox ("Usage: Parse.exe <OldText> <NewText>  <ParseFile>"  & Chr(13) & "Usage: Parse.exe <ReadVariablesFromFile> <ParseFile>", vbInformation, "Parse")
    WScript.Quit 
End If 

Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")

'*** CScript.exe Parse.vbs <ReadVariablesFromFile> <ParseFile>" ***
If (WScript.Arguments.Count = 2) Then 
    strReadVariablesFromFile = WScript.Arguments(0)
    strParseFile = WScript.Arguments(1)


    Set objReadVariablesFromFile = objFSO.OpenTextFile(strReadVariablesFromFile, ForReading)
    Set WshShell = WScript.CreateObject("WScript.Shell")

    Do Until objReadVariablesFromFile.AtEndOfStream
        strNextLine = objReadVariablesFromFile.Readline

        TMP = InStr(1, strNextLine, "=", 1)
        If Not TMP = "0" Then
        arrNextLine = Split(strNextLine, "=")
        strOldText = arrNextLine(0)  
        strNewText  = arrNextLine(1)
        SubReplace
        End If
    Loop
    objReadVariablesFromFile.Close
End If

'*** CScript.exe Repl.vbs <ParseFile> <OldText> <NewText> ***
If (WScript.Arguments.Count = 3) Then 
    strOldText = WScript.Arguments(0)
    strNewText = WScript.Arguments(1)
    strParseFile = WScript.Arguments(2)
SubReplace
End If


Sub SubReplace()
    Set objParseFile = objFSO.OpenTextFile(strParseFile, ForReading)
    strText = objParseFile.ReadAll
    objParseFile.Close

    strNewText = Replace(strText, strOldText, strNewText)
    Set objParseFile = objFSO.OpenTextFile(strParseFile, ForWriting)

    objParseFile.WriteLine strNewText
    objParseFile.Close
End Sub
Adapted for Script2EXE
Const ForReading = 1
Const ForWriting = 2

Function main(par)
main=0
'A console program should return 0 if no error

Dim Params, nParams
Params = Split(par, Chr(10), -1, 1)
nParams= UBound(Params) - LBound(Params) +1

If(nParams <> 2) And (nParams <> 3) Then 
    host.Print "Usage: Parse.exe <OldText> <NewText>  <ParseFile>"  & Chr(10) & "Usage: Parse.exe <ReadVariablesFromFile> <ParseFile>" & Chr(10)
    main=100
End If

Set objFSO = host.CreateObject("Scripting.FileSystemObject")

If (nParams = 2) Then 
    strReadVariablesFromFile = Params(0)
    strParseFile = Params(1)


    Set objReadVariablesFromFile = objFSO.OpenTextFile(strReadVariablesFromFile, ForReading)
    Set WshShell = host.CreateObject("WScript.Shell")

    Do Until objReadVariablesFromFile.AtEndOfStream
        strNextLine = objReadVariablesFromFile.Readline

        TMP = InStr(1, strNextLine, "=", 1)
        If Not TMP = "0" Then
            arrNextLine = Split(strNextLine, "=")
            strOldText = arrNextLine(0)  
            strNewText  = arrNextLine(1)
            SubReplace objFSO, strOldText, strNewText, strParseFile
        End If
    Loop
    objReadVariablesFromFile.Close
End If


If (nParams = 3) Then 
    strOldText = Params(0)
    strNewText = Params(1)
    strParseFile = Params(2)
    SubReplace objFSO, strOldText, strNewText, strParseFile
End If

End Function


Sub SubReplace(objFSO, strOldText, strNewText, strParseFile)
    Set objParseFile = objFSO.OpenTextFile(strParseFile, ForReading)
    strText = objParseFile.ReadAll
    objParseFile.Close

    strNewText = Replace(strText, strOldText, strNewText)
    Set objParseFile = objFSO.OpenTextFile(strParseFile, ForWriting)

    objParseFile.WriteLine strNewText
    objParseFile.Close
End Sub