将变量插入cmd命令

时间:2014-03-18 11:24:35

标签: vbscript cmd

我需要从用户(字符串)获取输入并将其插入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而不是值

2 个答案:

答案 0 :(得分:0)

以更有条理的方式构建命令 - 例如:

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
---------^^^

&#39;第二个想法&#39;补充 -

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

引用会更容易。

答案 1 :(得分:0)

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

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