Results 1 to 6 of 6

Thread: What's wrong with VBScript and Split?

  1. #1
    dfair
    Guest
    When I try to execute this very simple 3 line script I get Subscript out of range.
    What's up?

    ' vbscript test of Split Function
    dim a, b()
    a = "this is a test"
    b() = Split(a)


    C:\>cscript test.vbs
    Microsoft (R) Windows Script Host Version 5.1 for Windows
    Copyright (C) Microsoft Corporation 1996-1999. All rights reserved.

    C:\test.vbs(4, 1) Microsoft VBScript runtime error:

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    remove the paranthesis from the b.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    dfair
    Guest
    With the paranthesis removed from the b()...

    ' vbscript test of Split Function
    dim a, b()
    a = "this is a test"
    b = Split(a)

    Yeilds:

    C:\>cscript.exe test.vbs
    Microsoft (R) Windows Script Host Version 5.1 for Windows
    Copyright (C) Microsoft Corporation 1996-1999. All rights reserved.

    C:\test.vbs(4, 1) Microsoft VBScript runtime error: Type mismatch

    With the paranthesis in place I get:

    ' vbscript test of Split Function
    dim a, b()
    a = "this is a test"
    b() = Split(a)

    C:\>cscript.exe test.vbs
    Microsoft (R) Windows Script Host Version 5.1 for Windows
    Copyright (C) Microsoft Corporation 1996-1999. All rights reserved.

    C:\test.vbs(4, 1) Microsoft VBScript runtime error: Subscript out of range

    I'm beginning to think that Microsoft simply broke this function in VBScript.
    It works just fine for me in regular VB (with the paranthesis).

  4. #4
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Remove them from the dim as well.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  5. #5
    dfair
    Guest
    Thanks. Here's a VBS script that will modify ini files. It will change existing values, add new keys & values, or add new sections, keys and values. It can be run from the command line and requires four parameters: The ini file name, the section name, the key name and the key value.

    ' MODINI.vbs 4/10/01 D.Fair
    Option Explicit
    Dim INIfile, INIsect, INIkey, INIval
    Dim fs, Sect, Key, a, b, c, x, i
    Dim SectFound, KeyFound, Params, Stat
    set fs = CreateObject("Scripting.FileSystemObject")
    set Params=Wscript.Arguments
    If Params.Count <> 4 then
    Msgbox "You must specify four arguements for this script."
    else
    INIfile=Params.Item(0)
    INIsect=Params.Item(1)
    INIkey=Params.Item(2)
    INIval=Params.Item(3)
    End If
    Sect = "[" & Trim(INIsect) & "]"
    SectFound = False
    KeyFound = False
    ' Create the file if it does not exist and exit
    If fs.FileExists(INIfile) = False Then
    a = Sect & vbCrLf & INIkey & "=" & INIval & vbCrLf
    x = WriteFile(INIfile, a)
    Stop
    End If
    ' File does exist - get data
    a = ReadFile(INIfile)
    b = Split(a, vbCrLf)

    Do While i <= UBound(b)
    ' first check for the section of the ini file we're interested in.
    If Lcase(Sect) = Lcase(Trim(b(i))) Then ' do case insensitive compare
    SectFound = True ' rember we're in the section
    Sect = Trim(b(i)) ' insure correct case
    i = i + 1 ' advance to next aray element
    End If
    ' check to see if this aray element is the key we're looking for
    If mid(b(i),1,1) <> "]" AND SectFound = True Then
    x = Instr(1, b(i), "=") ' find key/value delimiter
    If x = 0 Then
    Key = b(i) ' whole element is the key
    Else
    Key = Mid(b(i), 1, x-1) ' extract the key
    End If
    If Lcase(Key) = Lcase(INIkey) Then ' do case insensitive compare
    KeyFound = True ' remember we've found the key
    b(i) = Key & "=" & INIval ' rewrite aray element with new value
    a = Join(b, vbCrLf) ' put aray back into string
    Stat = WriteFile(INIfile, a) ' rewrite the ini file
    KeyFound = True ' remember we've Created the key
    i = UBound(b) ' set index to end of loop
    End If
    End If
    ' check to see if we've passed through the section without finding the key
    If Mid(b(i),1,1) = "[" AND SectFound = True AND KeyFound = False Then
    ' skip back over any blank lines - no gaps in ini file
    While Trim(b(i-1)) = ""
    i = i - 1
    Wend
    Redim c(UBound(b)+1) ' create array with extra element
    For x = 0 to i-1
    c(x) = b(x) ' put previous elements into new array
    Next
    c(i) = INIkey & "=" & INIval ' rewrite aray element with new value
    For x = i To UBound(b)
    c(x+1) = b(x) ' write out the rest of the elements
    Next
    a = Join(c, vbCrLf) ' put aray back into string
    Stat = WriteFile(INIfile, a) ' rewrite the ini file
    KeyFound = True ' remember we've Created the key
    i = UBound(b) ' set index to end of loop
    End If
    i = i + 1
    Loop
    If SectFound = False Then
    ' If we get here the section was not found
    If Trim(b(Ubound(b))) = "" Then
    Redim Preserve b(UBound(b) + 2) ' create array with 2 extra elements
    Else
    Redim Preserve b(UBound(b) + 3) ' create array with 3 extra elements
    End If
    b(UBound(b)-1) = Sect ' write out the new section
    b(UBound(b)) = INIkey & "=" & INIval ' write out the new key & value
    a = Join(b, vbCrLf) ' put aray back into string
    Stat = WriteFile(INIfile, a) ' rewrite the ini file
    End If

    Function ReadFile(File2Read)
    Dim f, fs, ForReading
    ForReading = 1
    set fs = CreateObject("Scripting.FileSystemObject")
    ReadFile = ""
    If fs.FileExists(File2Read) = False then Exit Function
    Set f = fs.OpenTextFile(File2Read, ForReading, True)
    ReadFile = f.ReadAll
    f.Close
    End Function

    Function WriteFile(File2write, FileData)
    Dim f, fs, ForWriting
    WriteFile = False
    If File2Write = "" then Exit Function
    ForWriting = 2
    set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.OpenTextFile(File2Write, ForWriting, True)
    f.Write FileData
    f.close
    WriteFile = True
    End Function

  6. #6
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Cool, just in time to start phasing out the system's registry.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

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