0

In continuation to the script provided by rojo at escape double quotes in param file to batch script, after I have parsed the initial data file, I need to invoke a .vbs script from the batch. The .vbs script needs to be supplied with 2 of the tokens generated by parsing the initial data file. One of the token is a URL to a file on a server and another is the path on local disk. The .vbs script downloads the specified file specified by token one to local path specified by token two. What I want to do is to invoke the .vbs script in the script above and pass the tokens as parameters to it. myvbscript.vbs /FileURL:"https://abc.com/a.pdf" /HDLocation:"C:\a.pdf"

Here is the .bat file i have.

    @if(@a==@b) @end
/* :: batch portion
@ECHO OFF
setlocal if exist "%~1"
 ( cscript /nologo /e:jscript "%~f0" < "%~1" )
 else ( cscript /nologo /e:jscript "%~f0" )
 exit /b 
:: JScript portion */ 
while (!WSH.StdIn.AtEndOfLine) {
 var line=WSH.StdIn.ReadLine();
 var st_token = line.split('\t');
 var FileUR="abc.com/a.pdf";
 var HDLocation="C:\a.pdf"; 
WSH.Echo(req_id); 
WSH.Echo(att_tokens[i]);
 <<INVOKE VBSCRIPT WITH PARAMETERS>> 

I need to invoke vbscript in place of <<INVOKE VBSCRIPT WITH PARAMETERS>> Please help}

Please help me to invoke the .vbs script in the script above with passing tokens as parameters.

The .vbs script is as follows:

'Set your settings

Set colNamedArguments = WScript.Arguments.Named

strFileURL = colNamedArguments.Item("FileURL")
strHDLocation = colNamedArguments.Item("HDLocation")

' Fetch the file

Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()

If objXMLHTTP.Status = 200 Then
  Set objADOStream = CreateObject("ADODB.Stream")
  objADOStream.Open
  objADOStream.Type = 1 'adTypeBinary

  objADOStream.Write objXMLHTTP.ResponseBody
  objADOStream.Position = 0    'Set the stream position to the start

  Set objFSO = Createobject("Scripting.FileSystemObject")
    If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
  Set objFSO = Nothing

  objADOStream.SaveToFile strHDLocation
  objADOStream.Close
  Set objADOStream = Nothing
End if

Set objXMLHTTP = Nothing
4

1 回答 1

0

在您的文件中使用:

对于 .BAT 调用 .VBS

cscript //nologo [FILE.vbs] argsX argsY

对于 .JS 调用 .VBS

 wsShell = WScript.CreateObject("WScript.Shell");
 wsShell.run ("[FILE.VBS] argsX argsY");

您需要将这两个参数读入您的 .vbs 中,为此您可以使用:

Set args = WScript.Arguments
argsX = args.Item(0)
argsY = args.Item(1)

你的代码有这个,但我想我会记下这是如何为其他寻找类似解决方案的人完成的。

现在您可以像在代码中使用变量一样使用参数/参数。

.BAT 示例使用 testB.bat 进行了测试,其中包含以下代码行。

@ECHO OFF
cscript //nologo testv.vbs Hey There

.JS 示例使用 test.js 进行了测试,其中包含以下代码行。

wsShell = WScript.CreateObject("WScript.Shell");
wsShell.run ("testV.VBS Hey There");

testV.vbs 中的代码行如下。

Set args = WScript.Arguments
firstArg = args.Item(0)
secondArg = args.Item(1)
MsgBox(firstArg)
MsgBox(secondArg)

所有文件都存储在同一目录中。双击 test.js 或 testB.bat 文件会创建两个消息框。第一个说“嘿”,第二个说“那里”。

于 2013-04-22T16:32:20.443 回答