运行 VB 带参数的脚本文件

Running VB Script file with arguments

我必须 运行 一个有 2 个参数的 VB 脚本。所以我 运行 在下面执行命令。

Delete_Dummy1.vb
s C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv C:\Users\c6342\Deskto
p\XML_to_CSV\Temp_Files\xml1.txt
**VB Script Sample - not working:** 
**sourceloc** = WScript.Arguments.Item(0)
**destloc** = WScript.Arguments.Item(1)
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = objFSO.OpenTextFile("**sourceloc**")
Set newFile = objFSO.CreateTextFile("**destloc**")

它抛出错误 找不到文件。 但是如果我对 sourceloc 和 destloc 进行硬编码并删除参数,它就可以正常工作。只有当我使用参数时它才会抛出错误。

**Working VB Script sample:**
Set oTextStream = objFSO.OpenTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv")
Set newFile = objFSO.CreateTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt")

这很好用。但根据我的项目要求,我无法对这些文件位置进行硬编码。我可以从命令作为参数传递。

认为最好只更新我的答案。

参数不起作用的原因是您从未将它们传递给 OpenTextFile()CreateTextFile() 方法。相反,您传递的是包含变量名称而不是实际变量的文字字符串。

sourceloc = WScript.Arguments.Item(0)
destloc = WScript.Arguments.Item(1)

Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Pass the variables not a string literal
Set oTextStream = objFSO.OpenTextFile(sourceloc)
Set newFile = objFSO.CreateTextFile(destloc)

就目前而言,VBScript 不断尝试查找名为 sourcelocdestloc 的文件,而不是来自传递的 Arguments 集合的实际文件名。这可能导致

Microsoft VBScript Runtime Error: File Not Found


注:以下内容根据initial question修改而成


这取决于您将参数传递给脚本的方式,值中的任何空格都将被视为新参数。目前这是传递参数的方式;

0. C:\Users\enter
1. code
2. herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
3. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt

我确定这不是您所期望的。为避免这种情况,请将每个参数用双引号括起来 ("...").

Delete_Dummy1.vbs "C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv" "C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt"

这样你会得到更多你期望的

0. C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
1. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt