使用无效的过程调用或参数错误读取vbscript中的文件抛出错误

时间:2013-06-13 17:19:02

标签: vbscript

我有以下代码..如果我使用静态strInputPath3代码工作正常,但如果我使用strInputPath3代码错误输出错误无效的过程调用或参数..有人请告诉我我做错了什么这里

strInputPath1 =  "C:\test" & "\" & "css" & "\" & "main.css"

strInputPath3 = "C:\test\css\main.css"


Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)

2 个答案:

答案 0 :(得分:5)

如果您将VBScript可用作.OpenTextFile字符串的内容提供给该方法,该方法将尝试打开一个文件,并可能抛出“找不到文件”错误。

>> strInputPath1 =  "C:\test" & "\" & "css" & "\" & "main.css"
>> WScript.Echo strInputPath1
>> set f = goFS.OpenTextFile(strInputPath1,1)
>>
C:\test\css\main.css
Error Number:       76
Error Description:  Path not found

要获得“无效的过程调用”错误,您必须传递一些险恶的东西,例如:空值:

>> strInputPath1 = Empty
>> set f = goFS.OpenTextFile(strInputPath1,1)
>>
Error Number:       5
Error Description:  Invalid procedure call or argument

这些事实极有可能使你

  • 在其初始化及其在.OpenTextFile()
  • 中的使用之间更改了变量strInputPath1的内容。
  • 或初始化变量X并使用变量Y(Y& X可能是“strInputPath1”的变体)
  • 或初始化并在不同范围内使用两个相同名称的变量(~Functional / Subs)

使用“Option Explicit”启动脚本将降低此类错误的风险。

添加了wrt“将fso命名为错误”评论:

由于VBScript的错误消息通常难以理解/理解,这可能是一个很好的机会来反思“可能出现什么问题?VBScript会告诉我什么问题?我该怎么做才能解决错误?怎么能我将来会避免它吗?“

给出goFS中的第一个参数和一个拼写错误(=>空变量):

>> strInputPath1 =  "C:\test" & "\" & "css" & "\" & "main.css"
>> set f = goSF.OpenTextFile(strInputPath1,1)
>>
Error Number:       424
Error Description:  Object required

坚持理由:尝试在点的左侧没有对象的情况下调用方法(。操作符)是禁止的。

让我们将邪恶的goSF设置为一个对象:

>> Set goSF = New RegExp
>> set f = goSF.OpenTextFile(strInputPath1,1)
>>
Error Number:       438
Error Description:  Object doesn't support this property or method

仍然没有“无效的过程调用或参数”错误。由于goSF现在是一个RegExp,让我们忽略特定的方法(名称) - OpenTextFile() - 并看看如果我们搞砸了电话会发生什么:

>> WScript.Echo TypeName(goSF)
>> Set ms = goSF.Execute()
>>
IRegExp2
Error Number:       450
Error Description:  Wrong number of arguments or invalid property assignment
>> Set ms = goSF.Execute(Null)
>>
Error Number:       13
Error Description:  Type mismatch

所以我的主张仍然存在。错误“无效的过程调用或参数”是由将Empty提供给在有效FSO上调用的方法.OpenTextFile()引起的。

答案 1 :(得分:0)

这是一个老问题,但它今天让我感到困惑: 如果您尝试打开一个您认为是ASCII但实际上是Unicode的文件,OpenTextFile()也可以触发无效的过程调用。

请参阅https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx

所以

Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)

会变成

Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1, false, -1)