bat文件與Vbs文件之間的常用操作(獲取用戶輸入,執行VBS文件)
bat文件
set /P StrInput="輸入數字:" echo 輸入的數字為%StrInput% set /P Flg="是否執行(y/n):" IF "%Flg%" equ "y" ( echo 執行命令 cscript abc.vbs "%StrInput%" )
註意:
等於號(=)之間不能有空格,不然會出錯。
判斷值大小最好使用equ之類。
條件判斷後的括號的有空格。
VBS文件
獲取外部參數
寫文件
WebAPI操作
日期與TimeStamp變換
Dim WshShell Dim CurDir Dim oParam '取參數 Set oParam = WScript.Arguments if oParam.Count>0 Then Else WScript.Quit End if '獲取當前路徑 Set WshShell = WScript.CreateObject("WScript.Shell") CurDir = WshShell.CurrentDirectory '寫文件操作 Function OutputData(filename) Dim objFSOW Dim objFileW Set objFSOW = WScript.CreateObject("Scripting.FileSystemObject") Set objFileW = objFSOW.OpenTextFile(filename,2,True) objFileW.Write(filename) objFileW.Write(vbCrLf) objFileW.Write(vbTab) Set objFileW = Nothing Set objFSOW =Nothing End Function 'WebAPI操作 'params = "{""method"":""get"",""ID"":""12""}" Function RequestAPI(url,params) Dim oHttp Set oHttp = CreateObject("MSXML2.ServerXMLHTTP") on error resume next oHttp.Open "POST",url,False If Err Then RequestAPI = Err.Description End If On Error Goto 0 oHttp.SetRequestHeader "Content-Type","application/json" oHttp.Send params If oHttp.readyState<>4 Then oHttp.waitForResponse(10) End If RequestAPI = oHttp.ResponseText Set oHttp = Nothing End Function 'TimeStamp -> Date Function FormatDate(timestamp) FormatDate = DateAdd("s",CLng(timestamp),"01/01/1970 00:00:00") End Function 'Date ->TimeStamp Function DateToTimeStamp(dateValue) DateToTimeStamp = DateDiff("s","01/01/1970 00:00:00",dateValue) End Function
vbs服務器bat文件,window下批處理操作:bat文件中調用vbs
@echo "kill CRT process" taskkill /F /IM SecureCRT.exe ::ping 127.0.0.1 -n 30 echo "start vos through CRT" echo off d: cd D:\tools\SecureCRT_x86 start SecureCRT.EXE /SCRIPT D:\Secure_vos\Start_vos.vbs echo "start vos through CRT end" ping 127.0.0.1 -n 4 pause
上述代碼的意思是:在.bat文件中執行Start_vos.vbs文件
批處理執行文件之前首先需要下載SecureCRT.exe文件
Start_vos.vbs文件內容如下:
On Error Resume Next Dim result If crt.Session.Connected Then crt.Session.Disconnect ' connected to the '172.21.1.114' Set objtab = crt.Session.ConnectInTab("/SSH2 /PASSWORD root [email protected]",True) ' Capture error code and description (if any) nError = Err.Number strErr = Err.Description ' Now, tell the script host that it should handle errors as usual now: On Error Goto 0 ' change tab name objtab.caption = "Gdb_vos" ' enter the folder crt.Screen.Send "cd /home/test" & chr(13) crt.Screen.WaitForString chr(27) & "[32m(none) /home/test" & chr(27) & "[m # " End Sub
上述代碼主要是將手動輸入的過程使用VBS腳本進行自動化
首先:Set objtab = crt.Session.ConnectInTab("/SSH2 /PASSWORD root123 [email protected]",True)
采用SSH2的方式連接linuxIP地址172.21.1.114,密碼是root123,用戶名是rootobjtab.caption = "Start_vos"
將當前連接頁面重命名為Start_voscrt.Screen.Send "cd /home/test" & chr(13)輸入cd /home/test並回車
crt.Screen.WaitForString chr(27) & "[32m(none) /home/test" & chr(27) & "[m # "等待頁面下一行的內容為chr(27) & "[32m(none) /home/test" & chr(27) & "[m # "
到此這篇關於bat文件與Vbs文件之間的常用操作(獲取用戶輸入,執行VBS文件)的文章就介紹到這瞭,更多相關bat獲取用戶輸入,執行VBS文件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- securecrt中文亂碼怎麼辦 securecrt中文亂碼的解決方法
- SpringBoot實戰項目之谷歌瀏覽器全屏效果實現
- Python實戰之整蠱神器合集加速友盡
- SecureCRT的下載、安裝詳細過程
- PHP用Session實現用戶登陸功能