简体   繁体   English

将变量从批处理传递到VBS

[英]Pass variable from Batch to VBS

Hi I am trying to pass a user input variable from a BAT to VBS script 嗨,我正在尝试将用户输入变量从BAT传递到VBS脚本

Im sure this can be done in VBS but the user "Filename" input is used also later in the BAT file 我确定可以在VBS中完成此操作,但是稍后在BAT文件中也使用了用户“文件名”输入

As you can see the "FileName" variable in the .bat section needs to be passed into the VBS script for the file path ("C:\\Users\\bob\\Documents\\ %FileName%" ) 如您所见,.bat部分中的“ FileName”变量需要传递到文件路径(“ C:\\ Users \\ bob \\ Documents \\ %FileName%” )的VBS脚本中

.Bat 。蝙蝠

set /p FileName= Enter Filename Including Extention e.g. test.xlsx

VBS: VBS:

Set xlObj = CreateObject("Excel.Application")
Set xlFile = xlObj.WorkBooks.Open("C:\Users\xxx\Documents\%FileName%")
'turn off screen alerts
xlObj.Application.DisplayAlerts = False
'loop through sheets
For Each Worksheet In xlFile.Worksheets 
'change sheet to desired worksheet name
If Worksheet.Name = "Old111" Then
Worksheet.Name = "NewName111"
End if
Next
'save, close, then quit
xlFile.Close True
xlObj.Quit

I wouldn't recommend passing information from one script to another via environment variables. 我不建议通过环境变量将信息从一个脚本传递到另一个脚本。 I suppose you're running the VBScript from the batch script? 我想您是从批处理脚本运行VBScript吗? In that case you could simply pass the filename as an argument to the VBScript: 在这种情况下,您可以简单地将文件名作为参数传递给VBScript:

set /p "FileName= Enter Filename Including Extention (e.g. test.xlsx): "
cscript.exe //NoLogo C:\path\to\your.vbs "%FileName%"

In the VBScript you process the argument like this: 在VBScript中,您可以像这样处理参数:

filename = WScript.Arguments.Unnamed(0)

You could also use a name argument: 您还可以使用name参数:

set /p "FileName= Enter Filename Including Extention (e.g. test.xlsx): "
cscript.exe //NoLogo C:\path\to\your.vbs /filename:"%FileName%"

with the argument evaluation in your VBScript looking like this: VBScript中的参数求值看起来像这样:

filename = WScript.Arguments.Named("filename")

almost everything can be done with vbs. VBS几乎可以完成所有操作。 you don't need a batch 你不需要一批

to get user input from vbs, you can use stdin. 要从vbs获得用户输入,可以使用stdin。 copy from here . 这里复制。

' Read a single line into a variable
Dim strMyName
WScript.StdOut.Write("Enter Your full Name>")
WScript.StdIn.Read(0)
strMyName = WScript.StdIn.ReadLine()
WScript.StdOut.Write(strMyName)
WScript.CreateObject("WScript.Shell").ExpandEnvironmentStrings("%FileName%")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM