Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why Can't I Split A String 4

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
US
I am trying to split the string:

D:\PROGRA~1\COMMON~1\SYMANT~1\VIRUSD~1\20111208.001


I wrote the following code
Code:
  strKeyPath = "SOFTWARE\Symantec\SharedDefs"
  strValueName = "DEFWATCH_10"
  oReg.GetStringValue _
   HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
  strVirus = strValue 

  'Extract actual virus definition
  strArray = Split(strVirus,"\\",-1,1)
  strVirus = strArray(6)

  wscript.echo strVirus

But outputs

D:\PROGRA~1\COMMON~1\SYMANT~1\VIRUSD~1\20111208.001

Not

20111208.001


HELP!
 
You specified the split delimiter as "\\", it appears that your string does not contain this substring. If you intention is to split on the folders in the path, try changing your delimiter to "\".

Also, if your intent is to return the file name, strVirus = strArray(6), you may want to revisit your logic as depending on install options it may not always be in position 6. In fact, if the array is zero based, your example will be in position 5. You may want to check UBound(strArray) to reliably get the last element number.
 
I agree this is simplistic logic.

In this particular script, I know that there will always be 6 substrings.

By the way, I fixed the code

Code:
 'Extract actual virus definition
  strArray = Split(strVirus,"\",-1,1)
  strVirus = strArray(5)
  wscript.echo strVirus

and it now works!
 
>In fact, if the array is zero based, your example will be in position 5.

Split always produces a zero-based array no matter what the Option Base setting
 
Given that you won't always know the path depth, try:
Code:
'Extract the virus definition
strVirus = Split(strVirus, "\")(UBound(Split(strVirus, "\")))
wscript.echo strVirus

Cheers
Paul Edstein
[MS MVP - Word]
 
Another way, without Split:
strVirus=Mid(strValue,1+InStrRev(strValue,"\"))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top