下标超出了vbscript中的范围错误

时间:2013-06-17 16:27:55

标签: vbscript

有人可以查看下面的脚本并告诉我为什么它在vbscript中抛出这个错误下标超出范围错误。在文本文件中有两个条目它正确地写入文件但是它在退出时抛出错误循环,所以它永远不会调用其他函数..我认为它试图运行3次,但文本文件中只有2个条目

The text file is in this format

    Format.css Shared
    Design.css Shared


Dim strInputPath1
Dim txsInput1,txsOutput
Dim FSO
Dim Filename

Set FSO = CreateObject("Scripting.FileSystemObject")
strOutputPath = "C:\txt3.txt"
Set txsOutput = FSO.CreateTextFile(strOutputPath)

Set re = New RegExp
re.Pattern = "\s+"
re.Global  = True

Set f = FSO.OpenTextFile("C:\Users\spadmin\Desktop\Main\combination.txt")
Do Until f.AtEndOfStream
  tokens = Split(Trim(re.Replace(f.ReadLine, " ")))
  extension = Split(tokens(0),".")
  strInputPath1 =  "C:\inetpub\wwwroot\Test\files\" & tokens(1) & "\" & extension(1) & "\" & tokens(0) 
  Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)
  WScript.Echo strInputPath1
  txsOutput.Writeline txsInput1.ReadAll

Loop
WScript.Echo "Calling"

txsInput1.Close
txsOutput.Close
f.Close
Call CreateCSSFile()


''''''''''''''''''''''''''''''''''''
' Merge Css Files
''''''''''''''''''''''''''''''''''''
Sub CreateCSSFile()
 WScript.Echo "Called"

 Dim FilenameCSS

 Dim strInputPathCSS
 Dim txsInputCSS,txsOutputCSS
 Dim FSOCSS


Set FSOCSS = CreateObject("Scripting.FileSystemObject")
strOutputPathCSS = "C:\txt4.txt"
Set txsOutputCSS = FSOCSS.CreateTextFile(strOutputPath)

Set re = New RegExp
re.Pattern = "\s+"
re.Global  = True

Set fCSS = FSOCSS.OpenTextFile("C:\Users\spadmin\Desktop\TestingTheWebService\combination.txt")
Do Until fCSS.AtEndOfStream
  tokensCSS = Split(Trim(re.Replace(fCSS.ReadLine, " ")))
  extensionCSS = Split(tokensCSS(0),".")
  strInputPathCSS =  "C:\inetpub\wwwroot\EpsShared\c\" & tokensCSS(1) & "\" & extensionCSS(1) & "\" & tokensCSS(0) 
  Set txsInputCSS = FSOCSS.OpenTextFile(strInputPathCSS, 1)
  txsOutputCSS.Writeline txsInputCSS.ReadAll

Loop
fCSS.Close
txsInputCSS.Close
txsOutputCSS.Close
Set FSOCSS = Nothing
End Sub

1 个答案:

答案 0 :(得分:6)

如果您的文件包含尾随空白行,则应用Split()可能会返回少于2个元素的数组。在这种情况下,token(1)应该将'下标超出范围'错误。

如果Split()按预期工作,您应该始终检查:

tokens = Split(Trim(re.Replace(f.ReadLine, " ")))
If 1 = UBound(tokens) Then
   extension = Split(tokens(0),".")
   If 1 = UBound(extension) Then
      strInputPath1 =  "..." & tokens(1) & "..." 
   Else
      ... parse error ...
   End If
Else
   ... parse error or just trailing blank lines? ...
End If