vbscript中的下标超出范围错误 [英] subscript out of range error in vbscript

查看:38
本文介绍了vbscript中的下标超出范围错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone look at the below script and tell me why it's throwing this error subscript out of range error in vbscript ..In the text file there are two entries it writes to the file correctly but then it throws an error while exiting the loop so it never calls the other function..I think it's trying to run 3 times but there are just 2 entries in the text file

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

推荐答案

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

If your file contains trailing blank lines, applying Split() may return arrays with less than 2 elements. In that case token(1) should throw a 'subscript out of range' error.

您应该始终检查 Split() 是否按预期工作:

You should always check, if Split() workes as expected:

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

这篇关于vbscript中的下标超出范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆