2014-06-20 34 views
0

使用一個VBScript(只是一個例子)像我需要使用VBScript來控制一個命令行程序

result = MsgBox ("Would you like to install the AntrixAPI?", vbYesNo, "Installing AntrixAPI") 

Select Case result 
Case vbYes 
MsgBox("The API will be installed.") 
Case vbNo 
MsgBox("The API will not be install.") 
End Select 

我怎麼能使用它來控制一個命令行程序幫助。假設用戶選擇了是。那麼只有當用戶選擇是時,命令纔會到達某個點。 (示例命令)

@echo off 
:UserSelectedYes 
REM This is where the prompt would go if the user selected yes 
wget http://www.example.com/thisisafakedomain/api/antrix 
:UserSelectedNo 
REM This is where the prompt would go if the user selected no 
end 

這可能嗎?

+0

您可以試試[this](http://stackoverflow.com/a/21575421/2861476) –

+0

這正是我想要的。 100%謝謝SOOO很多 – SmokingNumbers

+0

可能的重複[將vbYesNoCancel的輸出傳遞到批處理中](http://stackoverflow.com/questions/21574341/passing-the-output-of-vbyesnocancel-into-a-batch) –

回答

0

你可以創建一個MS-DOS進程並執行每個腳本,你想!

就像這樣:

Dim oProcess As New Process 
With (oProcess.StartInfo) 
    .Arguments = "/k <command> & <command> & <command> & exit" 
    .FileName = "cmd.exe" 
    .WindowStyle = ProcessWindowStyle.Hidden 
    .CreateNoWindow = True 
End With 
oProcess.Start() 

它不是最好的,genteelst方式,但最快的。 我不知道,如果這是你想要的。因此,請隨時重新定義您的問題,但您不清楚您要求的是什麼!

+0

這是我想要的。比方說,我有一個MS-DOS命令,它調用一個'script.vbs'文件。該文件提示用戶是否/否框。如果用戶選擇'yes'選項,那麼'script.vbs'將會轉到與'script.vbs'相同的.bat/.cmd程序,並且將轉到某個標籤並且只運行該標籤。 – SmokingNumbers

+0

哦,你想用vbs? (我以爲你的意思是VB)這是一個問題,因爲,vbs無法使用Process類。那麼,你的問題變得更加複雜。你爲什麼不問問用戶,他在你的* .bat腳本中想要什麼? – Cydhra

+0

我已經考慮過這樣做了(這將會非常複雜),但.vbs腳本更方便用戶使用。 – SmokingNumbers

0

你的VBScript可以如此簡單:從您的批處理文件

WScript.Quit MsgBox("Would you like to install the AntrixAPI?", vbYesNo, "Installing AntrixAPI") 

調用它,並且測試返回值。 vbYes具有值6,vbNo有7

@echo off 
cscript c:\test.vbs 

if %errorlevel%==6 goto UserSelectedYes 
if %errorlevel%==7 goto UserSelectedNo 
+0

目前正在進行此測試。我會看看它是如何發展。 編輯:使用它,它打開了一個CMD窗口(在正確的目錄中),但它不執行我在標籤下設置的命令。它只是打開CMD窗口,等待用戶輸入一個命令 – SmokingNumbers

+0

我已經把這個放在'call test.vbs'中,我有它,它會給我一些錯誤,然後打開另一個CMD窗口等待用戶執行一個命令。 – SmokingNumbers

+0

您是否試圖返回到您的原始批處理文件?或者啓動一個新的? – Bond

0

值這是怎麼下載文件在VBScript。

On Error Resume Next 
Set File = WScript.CreateObject("Microsoft.XMLHTTP") 
File.Open "GET", "http://www.pepperresources.org/LinkClick.aspx?fileticket=B1doLYYSaeY=&tabid=61", False 
'This is IE 8 headers 
File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)" 
File.Send 
If err.number <> 0 then 
    line ="" 
    Line = Line & vbcrlf & "" 
    Line = Line & vbcrlf & "Error getting file" 
    Line = Line & vbcrlf & "==================" 
    Line = Line & vbcrlf & "" 
    Line = Line & vbcrlf & "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description 
    Line = Line & vbcrlf & "Source " & err.source 
    Line = Line & vbcrlf & "" 
    Line = Line & vbcrlf & "HTTP Error " & File.Status & " " & File.StatusText 
    Line = Line & vbcrlf & File.getAllResponseHeaders 
    wscript.echo Line 
    Err.clear 
    wscript.quit 
End If 

On Error Goto 0 

Set BS = CreateObject("ADODB.Stream") 
BS.type = 1 
BS.open 
BS.Write File.ResponseBody 
BS.SaveToFile "c:\users\test.txt", 2 
+0

這不是我想要的。由於彈出框,我只希望使用VBS提示用戶。我真的希望在CMD窗口中完成所有的功能。 – SmokingNumbers

+0

'選擇/ c yn/m「安裝ABC」''如果errorlevel 1 wget「http:// etc」''如果errorlevel 2轉到:eof' – phd443322

相關問題