简体   繁体   中英

Is there a way to make special characters work when using InStr in VBScript?

A VBScript is in use to shorten the system path by replacing entries with the 8.3 versions since it gets cluttered with how much software is installed on our builds. I'm currently adding the ability to remove duplicates, but it's not working correctly.

Here is the relevant portion of code:

original = "apple;orange;apple;lemon\banana;lemon\banana" 
shortArray=Split(original, ";")

shortened = shortArray(1) & ";"
For n=2 to Ubound(shortArray)
    'If the shortArray element is not in in the shortened string, add it
    If NOT (InStr(1, shortened, shortArray(n), 1)) THEN
        shortened = shortened & ";" & shortArray(n)
    ELSE
    'If it already exists in the string, ignore the element
        shortened=shortened
    End If
Next

(Normally "original" is the system path, I'm just using fruit names to test...)

The output should be something like

apple;orange;lemon\banana

The issue is entries with punctuation, such as lemon\\banana , seem to be skipped(?). I've tested it with other punctuation marks, still skips over it. Which is an issue, seeing how the system path has punctuation in every entry.

I know the basic structure works, since there are only one of each entry without punctuation. However, the real output is something like

apple;orange;lemon\banana;lemon\banana

I thought maybe it was just a character escape issue. But no. It still will not do anything with entries containing punctuation.

Is there something I am doing wrong, or is this just a "feature" of VBScript?

Thanks in advance.

This code:

original = "apple;orange;apple;lemon\banana;lemon\banana"
shortArray = Split(original, ";")

shortened = shortArray(0) ' array indices start with 0;  & ";" not here
For n=1 to Ubound(shortArray)
    'If the shortArray element is not in in the shortened string, add it
    'i.e. If InStr() returns *number* 0; Not applied to a number will negate bitwise
'   If 0 = InStr(1, shortened, shortArray(n), 1) THEN
    If Not CBool(InStr(1, shortened, shortArray(n), 1)) THEN ' if you insist on Not
        WScript.Echo "A", shortArray(n), shortened, InStr(1, shortened, shortArray(n), vbTextCompare)
        shortened = shortened & ";" & shortArray(n)
    End If
Next
WScript.Echo 0, original
WScript.Echo 1, shortened

WScript.Echo 2, Join(unique(shortArray), ";")

Function unique(a)
  Dim d : Set d = CreateObject("Scripting.Dictionary")
  Dim e
  For Each e In a
      d(e) = Empty
  Next
  unique = d.Keys()
End Function

output:

0 apple;orange;apple;lemon\banana;lemon\banana
1 apple;orange;lemon\banana
2 apple;orange;lemon\banana

demonstrates/explains your errors (indices, Not) and shows how to use the proper tool for uniqueness (dictionary).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM