从调用批处理脚本.VBS [英] invoke .vbs from batch script
问题描述
在延续由罗霍提供的脚本在<一个href=\"http://stackoverflow.com/questions/15970104/escape-double-quotes-in-param-file-to-batch-script\">escape在参数文件批处理脚本双引号,我已经分析的初始数据文件后,我需要从批次调用.vbs脚本。该.vbs脚本需要与通过解析原始数据文件生成的令牌2提供。一令牌的是一个URL到一个服务器上的文件,另一个是在本地磁盘的路径。该.vbs脚本下载由令牌两个指定的令牌一到本地路径指定指定的文件。
我想要做的就是调用上面的脚本的.vbs脚本并传递令牌作为参数传递给它。
myvbscript.vbs / FileURL: https://abc.com/a.pdf / HDLocation: C:\\ a.pdf
下面是.bat文件我有。
@if(@a == @ B)@end
/ * ::批部分
@ECHO OFF
SETLOCAL如果存在%〜1
(CSCRIPT / NOLOGO / E:JScript的%〜F0&LT;%〜1)
其他(CSCRIPT / NOLOGO / E:JScript的%〜F0)
退出/ B
:: JScript的部分* /
而(!WSH.StdIn.AtEndOfLine){
变种线= 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]);
&LT;&LT;具有参数&GT INVOKE VBSCRIPT;&GT;
我需要的地方调用VBScript的&LT;&LT;具有参数&GT INVOKE VBSCRIPT;&GT;
请帮助}
请帮我调用上面传递令牌作为参数在脚本的.vbs脚本。
该.vbs脚本如下:
'设置您的设置设置colNamedArguments = WScript.Arguments.NamedstrFileURL = colNamedArguments.Item(FileURL)
strHDLocation = colNamedArguments.Item(HDLocation)抓取文件设置objXMLHTTP =的CreateObject(MSXML2.XMLHTTP)objXMLHTTP.openGET,strFileURL,假
objXMLHTTP.send()如果objXMLHTTP.Status = 200然后
设置objADOStream =的CreateObject(的ADODB.Stream)
objADOStream.Open
objADOStream.Type = 1'adTypeBinary objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0'设置流的位置开始 设置objFSO =的CreateObject(Scripting.FileSystemObject的)
如果objFSO.Fileexists(strHDLocation)然后objFSO.DeleteFile strHDLocation
设置objFSO =没什么 objADOStream.SaveToFile strHDLocation
objADOStream.Close
设置objADOStream =什么
万一设置objXMLHTTP =什么
在您的文件使用:
有关.BAT调用.VBS
CSCRIPT // NOLOGO [FILE.vbs] argsX argsY
有关.JS调用.VBS
wsShell = WScript.CreateObject(WScript.Shell);
wsShell.run([FILE.VBS] argsX argsY);
您将需要两个参数读入到您的.VBS要做到这一点,你可以使用:
设置ARGS = WScript.Arguments
argsX = args.Item(0)
argsY = args.Item(1)
的您code的这一点,但我想我会记下这是如何正在为其他人寻找一个类似的解决方案做了。的
现在,您可以使用参数/参数,你会变量的code。
中的.bat例是使用testB.bat测试,内为code以下行。
@ECHO OFF
CSCRIPT // NOLOGO testv.vbs嘿
该.JS例是使用test.js测试中它是code以下行。
wsShell = WScript.CreateObject(WScript.Shell);
wsShell.run(testV.VBS嘿);
在testV.vbs的code线以下。
设置ARGS = WScript.Arguments
firstArg = args.Item(0)
secondArg = args.Item(1)
MSGBOX(firstArg)
MSGBOX(secondArg)
所有的文件被存储在同一目录中。双击test.js或testB.bat文件创建了两个消息框。第一个说第二个说:有。
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
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".
这篇关于从调用批处理脚本.VBS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!