3

有人可以看看下面的脚本并告诉我为什么它在 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
4

1 回答 1

8

如果您的文件包含尾随空行,则应用 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
于 2013-06-17T16:51:01.190 回答