使用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" est.vbs
cscript //nologo c:"test scripts" est.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

  

原文地址:https://www.cnblogs.com/mountain2011/p/9373858.html