将变量从批处理传递到VBS [英] Pass variable from Batch to VBS
问题描述
我正在尝试将用户输入变量从BAT传递到VBS脚本
Hi I am trying to pass a user input variable from a BAT to VBS script
我确信可以在VBS中完成此操作,但是稍后在BAT文件中也会使用用户文件名"输入
Im sure this can be done in VBS but the user "Filename" input is used also later in the BAT file
如您所见,.bat部分中的"FileName" 变量需要传递到文件路径的VBS脚本中("C:\ Users \ bob \ Documents \ %FileName%")
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
set /p FileName= Enter Filename Including Extention e.g. test.xlsx
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
推荐答案
我不建议通过环境变量将信息从一个脚本传递到另一个脚本.我想您是从批处理脚本运行VBScript吗?在这种情况下,您只需将文件名作为参数传递给VBScript:
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? In that case you could simply pass the filename as an argument to the VBScript:
set /p "FileName= Enter Filename Including Extention (e.g. test.xlsx): "
cscript.exe //NoLogo C:\path\to\your.vbs "%FileName%"
在VBScript中,您可以像这样处理参数:
In the VBScript you process the argument like this:
filename = WScript.Arguments.Unnamed(0)
您还可以使用name参数:
You could also use a name argument:
set /p "FileName= Enter Filename Including Extention (e.g. test.xlsx): "
cscript.exe //NoLogo C:\path\to\your.vbs /filename:"%FileName%"
在您的VBScript中的参数求值看起来像这样:
with the argument evaluation in your VBScript looking like this:
filename = WScript.Arguments.Named("filename")
这篇关于将变量从批处理传递到VBS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!