使用无效的过程调用或参数错误在 vbscript 中读取文件抛出错误 [英] Reading file in vbscript throwing error with invalid procedure call or argument error

查看:22
本文介绍了使用无效的过程调用或参数错误在 vbscript 中读取文件抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the below code..If I use the static strInputPath3 the code works fine but if I use the strInputPath3 the code errors out with an error invalid procedure call or argument..Can someone please tell me what I am doing wrong here

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

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


Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)

推荐答案

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

If you feed something that VBScript can use as a string to .OpenTextFile, the method will try to open a file and perhaps throw a "file not found" error.

>> 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

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

To get an "Invalid procedure call" error, you have to pass something sinister, e.g. an Empty value:

>> 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"的变体)
  • 或在不同范围内初始化并使用两个同名变量(~Functions/Subs)

使用Option Explicit"启动脚本将降低出现此类错误的风险.

Starting your scripts with "Option Explicit" will reduce the risk of such blunders.

添加了将 fso 命名为错误"的评论:

由于 VBScript 的错误消息通常难以解释/理解,因此这可能是一个很好的机会来反思什么地方会出错?VBScript 会告诉我有关问题的哪些信息?我应该怎么做才能修复错误?如何才能我以后会避免吗?"

As VBScript's error messages are often hard to interpret/understand, this may be a good opportunity to reflect on "What can go wrong? What will VBScript tell me about the problem? What should I do to fix the error? How can I avoid it in the future?"

在 goFS 中给定一个字符串的第一个参数和一个错字(=> 空变量):

Given a stringy first parameter and a typo (=> empty variable) in goFS:

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

有道理:尝试在没有点左侧对象的情况下调用方法(. 运算符)是不行的.

Stands to reason: Trying to call a method (. operator) without an object on the left of the dot is a no-no.

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

Let's Set the evil goSF to an object:

>> 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() - 看看如果我们搞砸了调用会发生什么:

Still no "invalid procedure call or argument" error. As goSF now is a RegExp, let's ignore the specific method(name) - OpenTextFile() - and see what happens if we mess up the call:

>> 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() 引起的.

So my claim still stands. The error "Invalid procedure call or argument" was caused by feeding Empty to the method .OpenTextFile() called on a valid FSO.

这篇关于使用无效的过程调用或参数错误在 vbscript 中读取文件抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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