开发者

How do I escape a semicolon in VB script?

I have a vbscript file that is reading a file and sending each line to a terminal program. When it comes to a semicolon in the middle of the string, it splits the semicolon at the string.

I have been using this code for quite sometime with other strings with no problems. There is one string per line in the file the script is reading.

The string in the file that is causing the problem is: 2101;99PSP

Here is the code I am using (with a terminal emulation program called Reflections):

Sub NarcoticOrderableItemTurnOff()

''# Constants used by OpenTextFile()
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const ICON_INFO = 64    ''# Information message; displays 'i' icon.

Set wshshell = CreateObject("WScript.Shell")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = _
        objFSO.OpenTextFile("P:\NarcoticOrderableItems.txt", ForReading)

Session.Transmit "^Orderable Item Edit (CPRS)" & vbCr

    Do Until objTextFile.AtEndOfStream

        strNextLine = objTextFile.ReadLine
        arrC2oderableItemList = Split(strNextLine, ";", 3)

        'arrServiceList(0) = Area of Use
        'arrServiceList(2) = Printer for that area of use

        With Session

        .WaitForString "Select ORDERABLE ITEMS NAME:"
        .Transmit arrC2oderableItemList(0) & vbCr
        .WaitForString "//"
开发者_高级运维        .Transmit "N" & vbCr
        .WaitForString "//"
        .Transmit vbCr
        .WaitForString "//"
        .Transmit vbCr
        .WaitForString "//"
        .Transmit vbCr
        End With

    Loop

objTextFile.close

Session.MsgBox "All done!  C2 Orderable Items turned off!", vbExclamation

''#ErrorHandler:
''#    Session.MsgBox Err.Description, vbExclamation + vbOKOnly

End Sub


I think it might have something with the following row of code to do:

arrC2oderableItemList = Split(strNextLine, ";", 3)


If this problem string is the whole line from the file you're reading:

2101;99PSP

The problem is that you're trying to get 3 items from every line and this one only has 2. To account for lines that don't have a 3rd item you should remove the 3rd parameter from your Split function and then check the UBound of the Array before using the 3rd item.

arrC2oderableItemList = Split(strNextLine, ";")

If UBound(arrC2oderableItemList) >= 2 Then
  ''# There are 3 items or more in the Array (O-based)
  ''# Can do something with arrC2oderableItemList(2)
Else
  ''# There are only 2 items (or less) in the Array
  ''# Do not use arrC2oderableItemList(2)
End If


If you are splitting the lines at semicolons but the text contains extra semicolons, you would have to

  1. search for extra semicolons
  2. change them to a pattern that would not normally be found in the text
  3. split your line
  4. change the pattern from step 2 back to semicolons

Or take the easy route and don't allow extra semicolons in the files your reading.

Posting a few example lines (including the problem line) would allow somebody to help you in writing code to search for the extra semicolons.

If (2101;99PSP) is all that is on the line see Shawn Steward's answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜