Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VBScript runtime error: Subscript out of range

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,49042
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript runtime error: Subscript out of range
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 VBScript runtime error: Subscript out of range - 7/4/2007 6:50:42 PM   
  nicci

 

Posts: 5
Score: 0
Joined: 7/4/2007
Status: offline
When I execute the script below I get Following error (with parameters)
"D:\Data\My_Progs\TA_Control_Center\Tools\UpdaterSoft\_Other\Rtask(beta).vbs(15, 3) Microsoft VBScript runtime error: Subscript out of range"

That is on the first line of code inside the for next loop "If InStr(LCase(args.Item(k)),"/f:") > 0 then".

I also tried replacing the "If's" inside the loop with "Select Case" but it generates the same error


When I  add "On Error Resume Next" it works perfectly.
I can't find any fault in the source or am I looking over it?




CODE:

Set args = WScript.Arguments

dissablefilter= 0
'0 = show all
'1 = show only disabled
'2 = show only enabled

WScript.Echo "args: " & args.Count

'# Read & process arguments
If args.Count>0 Then

   For k = 0 To args.Count

       If InStr(LCase(args.Item(k)),"/f:") > 0 then
           filtr=mid(args.Item(k),4)
       ElseIf instr(LCase(args.Item(k)),"/ou:")>0 then
           ou=ou & "ou=" & mid(args.Item(k),5) & ", "
       ElseIf instr(LCase(args.Item(k)),"/dc:")>0 then
           dc=dc & "dc=" & mid(args.Item(k),5) & ", "
       ElseIf instr(LCase(args.Item(k)),"/cn:")>0 then
           cn=cn & "cn=" & mid(args.Item(k),5) & ", "
       ElseIf instr(LCase(args.Item(k)),"/disfilt:")>0 then
           dissablefilter= mid(args.Item(k),10)
       ElseIf instr(LCase(args.Item(k)),"/list:")>0 then
           PClist= mid(args.Item(k),7)
       end if

   Next

   Wscript.Echo "ou: " & ou
   Wscript.Echo "cn: " & cn
   Wscript.Echo "dc: " & dc
   Wscript.Echo "filter: " & filtr
   Wscript.Echo "dissablefilter: " & dissablefilter
   Wscript.Echo "PClist: " & PClist
   Wscript.Echo

End if


WScript.Quit

 
 
Post #: 1
 
 RE: VBScript runtime error: Subscript out of range - 7/4/2007 7:05:29 PM   
  ginolard


Posts: 1053
Score: 21
Joined: 8/10/2005
Status: offline
What's the command line you are running when you get this error?

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to nicci)
 
 
Post #: 2
 
 RE: VBScript runtime error: Subscript out of range - 7/4/2007 7:17:32 PM   
  nicci

 

Posts: 5
Score: 0
Joined: 7/4/2007
Status: offline
cscript.exe script.vbs /list:C:\temp\list


It doesn't matter what parameter (arg) I use

(in reply to ginolard)
 
 
Post #: 3
 
 RE: VBScript runtime error: Subscript out of range - 7/4/2007 9:01:32 PM   
  ginolard


Posts: 1053
Score: 21
Joined: 8/10/2005
Status: offline
Is there only ever going to be one argument passed to this script?  What is the script meant to be doing?

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to nicci)
 
 
Post #: 4
 
 RE: VBScript runtime error: Subscript out of range - 7/4/2007 10:31:37 PM   
  nicci

 

Posts: 5
Score: 0
Joined: 7/4/2007
Status: offline
The amount of arguments is not fixed.

The script seems to works 100% fine when I add "On Error Resume Next" at the beginning of the script

The script is meant to perform tasks on range of PCs
those PC's can be retrieved from a textfile or from Active directory (PC's from a certain container)

Between the last "Enf If" and " WScript.Quit" is the code that

FYI here is the complete code


      

(in reply to ginolard)
 
 
Post #: 5
 
 RE: VBScript runtime error: Subscript out of range - 7/5/2007 12:13:48 AM   
  ebgreen


Posts: 5035
Score: 31
Joined: 7/12/2005
Status: online
Your args are in the proper format to use the Named arguments collection. Then you would not need to use any counts. Look at the WSH docs for Named Arguments.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to nicci)
 
 
Post #: 6
 
 RE: VBScript runtime error: Subscript out of range - 7/5/2007 7:15:56 AM   
  Skie

 

Posts: 58
Score: 0
Joined: 3/2/2006
Status: offline
This should fix your subscript out of range error:
 For k = 0 To args.Count -1

(in reply to nicci)
 
 
Post #: 7
 
 RE: VBScript runtime error: Subscript out of range - 7/5/2007 9:01:42 PM   
  nicci

 

Posts: 5
Score: 0
Joined: 7/4/2007
Status: offline
thx skie
that resolved the error

(in reply to Skie)
 
 
Post #: 8
 
 RE: VBScript runtime error: Subscript out of range - 7/7/2008 7:30:14 PM   
  ridzz

 

Posts: 1
Score: 0
Joined: 7/7/2008
Status: offline
Hi All.
I face the same problem when running my vb script.

The script is to read all the content in one particular folder and take the character from this symbol '=' to this symbol ';' and put those into an array. there are 2 array involve. one is an outer array for store char after the trunking process is complete. the inner array is to store char before the trunking process is done. when i run the script, it will throw me an error after succesfully process 3 text files. error thrown is - Microsoft VBScript runtime error: Subscript out of rang e: '[number: 0]'. the error will thrown when executing this line - Wscript.Echo "outerArray 0 " & outerArray(0). if i take out those line it will run smoothly. however i want to pass the value in the innerarray to objconnection line below which is outside the loop. is someone can help me? thanks in advance

below is the script:

Const ForReading = 1
Const adOpenStatic = 3
Const adLockOptimistic = 3

strFolderName = "D:\db\"
strREPattern = ".txt"

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
Set objFS = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFS.GetFolder(strFolderName)
Set objRE = new RegExp

objRE.Pattern = strREPattern
objRE.IgnoreCase = True

For Each objFile In objFolder.Files

   Set objTextFile = objFS.OpenTextFile(objFile , ForReading)

   Wscript.Echo objFile

   Do Until objTextFile.AtEndOfStream
       strNextLine = objTextFile.Readline
       outerArray = Split(strNextLine , ";")
       For i = 0 to Ubound(outerArray)
       innerArray = Split(outerArray(i) , "=")
       For x = 1 to Ubound(innerArray)
           Wscript.Echo "innerArray: " & innerArray(x)
           outerArray(i) = innerArray(x)
       Next
       Next

       Wscript.Echo "outerArray 0 " & outerArray(0)
       Wscript.Echo "outerArray 1 " & outerArray(1)
       Wscript.Echo "outerArray 2 " & outerArray(2)
       Wscript.Echo "outerArray 3 " & outerArray(3)
       Wscript.Echo "outerArray 4 " & outerArray(4)

'    objConnection.Open _
'        "Provider= outerArray(0) ; Data Source= outerArray(1) ;" & _
'            "Trusted_Connection=Yes ; Initial Catalog= outerArray(4) ;" & _
'                 "User ID= outerArray(2) ; Password= outerArray(3) ;"

'objRecordSet.Open "SELECT * FROM Customers", _
'        objConnection, adOpenStatic, adLockOptimistic

'objRecordSet.MoveFirst

'Wscript.Echo objRecordSet.RecordCount



    Loop

Next

(in reply to nicci)
 
 
Post #: 9
 
 RE: VBScript runtime error: Subscript out of range - 7/8/2008 9:39:21 AM   
  nicci

 

Posts: 5
Score: 0
Joined: 7/4/2007
Status: offline
try replacing:

      

by:


      

The problem is probably due to the contents of the files you're reading
they probably contain some lines with less than 5 items separated with ";"
then you'll get errors because you try to readout an outerarray item that doesn't contain data.

If your goal is to read all TXT files from strFolderName
I think the filter you're using won't work.
You created an RegExp but I think you have to execute it also.

I'm not even sure you can use an RegExp to filter filetypes.
Correct me if I'm wrong

(in reply to ridzz)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript runtime error: Subscript out of range Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts