将变量从批处理传递到 VBS [英] Pass variable from Batch to VBS

查看:28
本文介绍了将变量从批处理传递到 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" 变量需要传递到文件路径 ("C:UsersobDocuments%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:UsersobDocuments %FileName%")

.蝙蝠

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

VBS:

Set xlObj = CreateObject("Excel.Application")
Set xlFile = xlObj.WorkBooks.Open("C:UsersxxxDocuments\%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	oyour.vbs "%FileName%"

在 VBScript 中,您像这样处理参数:

In the VBScript you process the argument like this:

filename = WScript.Arguments.Unnamed(0)

您也可以使用名称参数:

You could also use a name argument:

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

VBScript 中的参数求值如下所示:

with the argument evaluation in your VBScript looking like this:

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

这篇关于将变量从批处理传递到 VBS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆