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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array out of range

Status
Not open for further replies.

LeighTT

Technical User
Feb 12, 2003
15
0
0
GB
I am new to Vbscript and have come up against a problem using an array to split a string. The error is (subscript out of range). The error occurs after reading the 10th line of text. I have been looking at ways to destroy and recreate the array before looping back but cannot see a way to do this any suggestions welcome?????

Dim MyString, myarray, objfso, objtextfile
set objfso = createobject("scripting.filesystemobject")

set objtextfile = objfso_OpenTextFile ("c:\shares2.txt")
Do while not objTextFile.AtEndOfStream
Mystring = objTextFile.Readline
MyArray = Split(MyString,"=", -1, 1 )
wscript.Echo myarray (0)
wscript.Echo myarray (1)
loop
 
use a UBound.

something like

If UBound(MyArray) = 2 Then
wscript.echo MyArray(0)
wscript.echo MyArray(1)
Else
wscript.echo "line didnt contain an ="
wscript.echo "or line contains more than one ="
End If

or do something like
If Instr(MyString, "=") Then.......

sounds like your line in the source files arent correct or you don thave error correction in place to handle if they arent correct

von moyla


 
The source file is the share info pulled out of the Registry and is in the form shown below. What I am trying to do is to extract the sharename and Share path and then check for the existance of the directory. The complete script is as follows.

Dim MyString, myarray, myarray2, myarray3, myarray4, Msg, strShareName, strSharePath, objfolder2, fso, obshell
set objfso = createobject("scripting.filesystemobject")
set objshell = createobject("wscript.shell")
'objshell.Run "%Comspec% /C Reg query HKLM\system\currentcontrolset\services\lanmanserver\shares \\server1 > c:\shares.txt"

set objtextfile = objfso_OpenTextFile ("c:\shares.txt")

Do while not objTextFile.AtEndOfStream
Mystring = objTextFile.Readline

''''''''' The following statements split the string up into arrays
MyArray = Split(MyString,"=", -1, 1 )

mystring2 = myarray (2)
myarray2 = split(Mystring2,";", -1, 1)

mystring3 = myarray (0)
myarray3 = split(mystring3,"maxuses", -1,1)

mystring4 = myarray3 (0)
myarray4 = split(mystring4,"MULTI_SZ", -1,1)

strSharePath = Myarray2 (0)
strShareName = Myarray4 (1)

''''''''''' Myarray4 (1) = sharename
''''''''''' Myarray2 (0) = share path

if objfso.FolderExists(strsharepath) then
wscript.Echo "This folder exists"
else
wscript.Echo "This folder does not exist"
end if

loop

: Below is example of source file

MULTI_SZ REPO$ MaxUses=4294967295;Path=C:\PROGRA~1\NETWOR~1\MANAGE~1\Disks;Permissions=7;Remark=NTME Repository;Type=0
MULTI_SZ MEUPGD$ MaxUses=4294967295;Path=C:\PROGRA~1\NETWOR~1\MANAGE~1\Disks\Upgrades;Permissions=7;Remark=NTME Update Daemon Directory;Type=0
MULTI_SZ ExtraDAT MaxUses=4294967295;Path=D:\Extra DAT;Permissions=0;Remark=;Type=0
MULTI_SZ Quarantine MaxUses=4294967295;Path=D:\Quarantine;Permissions=127;Type=0
MULTI_SZ MENPClient32 MaxUses=4294967295;Path=C:\Program Files\Network Associates\Management Console\NP250\32;Permissions=127;Type=0
MULTI_SZ SuperDAT MaxUses=4294967295;Path=D:\Super DAT;Permissions=0;Remark=;Type=0
MULTI_SZ Alerts MaxUses=4294967295;Path=D:\Alerts;Permissions=127;Remark=Anti Virus Alerts;Type=0
MULTI_SZ RLog MaxUses=4294967295;Path=D:\Remote User Logs;Permissions=127;Remark=Remote Access Log File;Type=0
MULTI_SZ DRSOLSAVTK MaxUses=4294967295;Path=D:\Dr.Sols AVTK\8.02 95;Permissions=127;Type=0
MULTI_SZ MCON MaxUses=4294967295;Path=C:\Program Files\Network Associates\Management Console;Permissions=127;Type=0
MULTI_SZ deploy$ MaxUses=4294967295;Path=C:\Program Files\Network Associates\Batch;Permissions=0;Type=0
MULTI_SZ SPM2000C$ MaxUses=4294967295;Path=C:\;Permissions=127;Remark=Created by SPM2000;Type=2147483648
 
seeing as the file is actually delimited with ";" i would start with these and not the = signs

set fso = CreateObject("Scripting.FileSystemObject")
set objtextfile = fso_OpenTextFile ("d:\vbs\test\dump.txt")

Do while not objTextFile.AtEndOfStream
Mystring = objTextFile.Readline

myarray2 = split(Mystring,";", -1, 1)

'msgbox myarray2(0)
'msgbox myarray2(1)
strSharePath = Right(myarray2(1), Len(myarray2(1)) - InstrRev(myarray2(1), "="))
'msgbox strSharePath


if fso.FolderExists(strsharepath) then
wscript.Echo "This folder exists"
else
wscript.Echo "This folder does not exist"
end if
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top