使用Wscript/cscript調用VB腳本


●強制用Wscript.exe執行

SET Wshell=CreateObject("Wscript.Shell")

if lcase(right(Wscript.fullName,11)) = "cscript.exe" then
Wshell.run "wscript.exe //nologo " & chr(34) & wscript.scriptfullname & chr(34)
Wscript.quit
else
Wscript.echo "執行代碼寫在此處"
end if


●強制用Cscript.exe執行:

SET Wshell=CreateObject("Wscript.Shell")

if lcase(right(Wscript.fullName,11)) = "wscript.exe" then

Wshell.run "cmd /k cscript.exe //nologo " & chr(34) & wscript.scriptfullname & chr(34)
Wscript.quit
else
Wscript.echo "執行代碼寫在此處"
end if

●調試:
在cmd窗口輸入:
Wscript.exe /X test.vbs
會彈出visual studio調試窗口,即可完成調試。

或者:
利用消息框調試:
WScript.Sleep(10000) //sleep 10秒
WScript.Echo "Success" //在wscript.exe下顯示消息彈框
Wshell.Popup "aaabbb" //在cscript.exe下顯示消息彈框

●bat腳本調用vb方式

cscript //logo c:\"test scripts"\test.vbs
cscript //nologo c:\"test scripts"\test.vbs

 

cmd /k表示 執行完后面的命令時,窗口保留,/c表示窗口關閉

/后面表示選項,常用用下列幾種,你可以在命令行下輸入 cmd /?看到:

/C 執行字符串指定的命令然后終斷
/K 執行字符串指定的命令但保留
/S 在 /C 或 /K 后修改字符串處理(見下)
/Q 關閉回應
/D 從注冊表中停用執行 AutoRun 命令(見下)
/A 使向內部管道或文件命令的輸出成為 ANSI
/U 使向內部管道或文件命令的輸出成為 Unicode

●VBScript無效字符800A0408編譯錯誤
①在記事本中打開.vbs
②轉到文件並“另存為”
③在文件名框下面,編碼下拉菜單選擇ANSI。

//強制使用cscript.exe執行vb腳本。	
Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
Set wshShell = CreateObject( "WScript.Shell" )
strEngine = UCase( Right( WScript.FullName, 12 ) )

If strEngine <> "\CSCRIPT.EXE" Then
	' Recreate the list of command line arguments
	strArgs = ""
	If WScript.Arguments.Count > 0 Then
		For i = 0 To WScript.Arguments.Count - 1
			strArgs = strArgs & " " & WScript.Arguments(i)
		Next
	End If

	' Create the complete command line to rerun this script in CSCRIPT
	strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs

	' Rerun the script in CSCRIPT
	Set objDebug = wshShell.Exec( strCmd )

	' Wait until the script exits
	Do While objDebug.Status = 0
		WScript.Sleep 100
	Loop

	' Exit with CSCRIPT's return code
	WScript.Quit objDebug.ExitCode
End If
	
//強制使用cscript.exe執行vb腳本。	
	
If (right(Ucase(WScript.FullName),11)="WSCRIPT.EXE") Then
    Dim WshShell,args,objArgs,I
    Set WshShell = CreateObject("WScript.Shell")
    args=""
    If Wscript.Arguments.Count > 0 Then
        Set objArgs = WScript.Arguments
        For I = 0 to objArgs.Count - 1
            args = args & " " & objArgs(I)
        Next
    End If
    Set objDebug = WshShell.Exec ( "cscript.exe /NoLogo """ & Wscript.ScriptFullName & """" & args )
 
    ' Wait until the script exits
    Do While objDebug.Status = 0
        WScript.Sleep 100
    Loop
 
    ' Exit with CSCRIPT's return code
    WScript.Quit objDebug.ExitCode
End If

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM