invoke .vbs from batch script

ぐ巨炮叔叔 提交于 2019-12-25 01:55:15

问题


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

回答1:


In your file use:

For .BAT calling .VBS

cscript //nologo [FILE.vbs] argsX argsY

For .JS calling .VBS

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

You will need to read the two parameters into your .vbs and to do that you can use:

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

Your code has this but I thought I would make a note of how this is being done for anyone else looking for a similar solution.

Now you can use the arguments/parameters as you would variables in your code.

The .BAT example was tested using testB.bat and within is was the following lines of code.

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

The .JS example was tested using test.js and within it was the following lines of code.

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

The code lines in testV.vbs are the following.

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

All files are stored in the same directory. Double clicking the test.js or the testB.bat file creates two message boxes. The first says "Hey" and the second says "There".



来源:https://stackoverflow.com/questions/16151876/invoke-vbs-from-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!