【问题标题】:insert a variable to cmd command在 cmd 命令中插入一个变量
【发布时间】:2014-03-18 11:24:34
【问题描述】:

我需要从用户那里得到一个输入(字符串)并将其插入到一个 cmd 命令中,

Set oShell = WScript.CreateObject("WScript.Shell")  
Set LabelName = WScript.CreateObject("WScript.Shell") 

LabelName = InputBox("Please Enter Label to check-out:", _
  "Create File")
oShell.run "cmd /K ""c:\Program Files (x86)\Borland\StarTeam Cross-Platform Client 2008   R2\stcmd.exe"" co -p bla:bla123@123.com:7777/bla/ -is -eol on -o -rp D:\ST_test -cfgl  3.1.006"

输入是“LabelName”,它应该插入而不是“3.1.006”

我无法插入这个变量,它一直在插入 LabelName 而不是值

【问题讨论】:

  • 那你的问题是什么?
  • 这是一个常见的错误,在tag wiki 中有介绍。 VBScript 不会在字符串中展开变量。相反,您需要将变量与字符串的两半连接起来。

标签: vbscript cmd


【解决方案1】:

LabelName 不需要是 shell 对象,如 it's just a string。然后将字符串连接到运行命令上,就完成了。

Set oShell = WScript.CreateObject("WScript.Shell")  
Dim LabelName

LabelName = InputBox("Please Enter Label to check-out:", _
  "Create File")
oShell.run "cmd /K ""c:\Program Files (x86)\Borland\StarTeam Cross-Platform Client 2008 R2\stcmd.exe"" co -p bla:bla123@123.com:7777/bla/ -is -eol on -o -rp D:\ST_test -cfgl  " & LabelName

【讨论】:

  • 好的,但是在这段代码中,这个命令没有运行,需要在某处引用
  • 很可能是 2008 年和 R2 之间的三倍空间。试试看。除此之外,您会大致了解它。
【解决方案2】:

以更结构化的方式构建您的命令 - 例如:

Option Explicit

Function qq(s) : qq = """" & s & """" : End Function

Dim sLabel : sLabel   = "default label"
Dim oWAU   : Set oWAU = WScript.Arguments.Unnamed
If 1 <= oWAU.Count Then sLabel = oWAU(0)
Dim sCmd   : sCmd     = Join(Array( _
     "%comspec%" _
   , "/K" _
   , qq("c:\Program Files (x86)\Borland\StarTeam Cross-Platform Client 2008   R2\stcmd.exe") _
   , "co" _
   , "-p bla:bla123@123.com:7777/bla/" _
   , "-is -eol on -o -rp" _
   , qq(sLabel) _
))
WScript.Echo sCmd

并显示额外的变量 sCmd。修复(可能的)错误 -

ient 2008   R2\stcm
---------^^^

'重新考虑'补充 -

, qq(sLabel) _
==>
, qq("D:\ST_test") _
, "-cfgl" _
, sLabel _

而且报价会容易得多。

【讨论】:

  • 不好意思,这个没看懂,能解释一下吗?
  • -1 答案令人困惑,没有解决源代码之外的原始问题,没有解释即​​兴执行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多