本帖最后由 loquat 于 2013-12-15 01:56 编辑
- :On Error Resume Next
- Sub bat
- echo off & cls
- echo Batching_codez_here_following_vbs_rules & pause
- start wscript -e:vbs "%~f0"
- Exit Sub
- End Sub
- MsgBox "This is vbs"
复制代码
- ::' 带参数的版本
- :On Error Resume Next
- :Sub bat
- echo off & cls
- echo Batching_codez_here_following_vbs_rules & pause >nul
- echo '>nul & start "" wscript //e:vbscript "%~f0" %*
- Exit Sub : End Sub
- MsgBox "This is vbs"
- for each i in wscript.arguments
- wscript.echo i
- next
复制代码
- ::' 另一种方案的混合
- ::' 源网址http://hi.baidu.com/paktc/item/27c33e3b1cf831be603aff97
- ' 2>nul &echo off &cls &fsutil file setshortname "%~nx0" "%~n0.vbs"
- :'loop
- ' 2>nul &set /p inp="你可以输入两个小数,本脚本尝试作乘法运算: "
- ' 2>nul &if "%inp%" == "exit" (exit)
- ' 2>nul &cscript //nologo "%~n0.vbs" %inp%
- ' 2>nul &pause &cls &goto :'loop
- set x=wscript.arguments
- call count(x(0),x(1))
- sub count(a,b)
- wscript.echo a*b
- wscript.quit
- end sub
复制代码
- 'vbs版cmd
- '可执行bat,也可以执行vbs
- 'By zqz0012005@www.bathome.cn
- 'Version: 0.3 2008-06-18
- On Error Resume Next
- host = WScript.FullName
- f0 = WScript.ScriptFullName
- dp0 = Left( f0, InstrRev(f0,"\") )
-
- if LCase( right(host, len(host)-InStrRev(host,"\")) ) = LCase("wscript.exe") then
- CreateObject("WScript.Shell").run "cmd /d /ccolor f9&title vbs-cmd" &_
- "&cscript //logo """ & f0 & chr(34)
- WScript.Quit
- end if
-
- set ws = CreateObject("WScript.Shell")
- set wn = CreateObject("WScript.Network")
- set fso = CreateObject("Scripting.FileSystemObject")
- set dict = CreateObject("Scripting.Dictionary")
- set sh = CreateObject("Shell.Application")
- set wmi = GetObject("winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2")
-
- 'set colps = wmi.instancesOf("win32_process")
- 'set colps = GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
-
- set stdin = WScript.stdin
- set stdout = WScript.stdout
- stdout.writeline "可以直接运行vbs语句,要运行cmd命令,请在命令前加'cmd '(cmd空格)" '::现版本没有这条限制
-
- do
- stdout.writeBlankLines(1)
- stdout.write ws.CurrentDirectory & vbCrLf & "# "
- s=Trim(stdin.readLine)
- Select Case 0
- Case StrComp(s,"exit",1) wsh.quit
- Case StrComp(left(s,2),"cd",1)
- if mid(s,3)="" then
- wsh.echo ws.CurrentDirectory
- elseif mid(s,3,1)=" " then
- ws.CurrentDirectory = LTrim(mid(s,3))
- else
- execStatementBlock
- end if
- Case StrComp(left(s,4),"cmd ",1)
- set oexec=ws.exec( "cmd /d /q /c" & LTrim(mid(s,4)) & "&exit" )
- 'oexec.stdin.writeLine vbCrLf & "exit"
- stdout.write oexec.stdout.readall & oexec.stderr.readall
- set oexec=nothing
- Case StrComp(left(s,5),"wmic ",1)
- set oexec=ws.exec("cmd /d /q /c echo exit|" & s)
- 'oexec.stdin.writeLine vbCrLf & "exit"
- stdout.write oexec.stdout.readall & oexec.stderr.readall
- set oexec=nothing
- Case StrComp(s,"help",1) call help()
- Case StrComp(s,"") 'do nothing
- Case Else
- LastCode = execStatementBlock()
- 'Case Else execute s
- 'Case Else wsh.echo "[" & s & "]"
- End Select
- showErrMsg
- loop
-
- function execStatementBlock()
- dim i, t
- ReDim a(0)
- a(0) = s
- stdout.write "More? "
- t = Trim(stdin.readLine)
- do until t=""
- i = i + 1
- ReDim Preserve a(i)
- a(i) = t
- stdout.write "More? "
- t = Trim(stdin.readLine)
- loop
- execute Join(a, ":")
- execStatementBlock = Join(a, vbCrLf)
- end function
-
- sub help()
- stdout.writeline vbCrLf & "可以直接运行vbs语句,在More? 提示符后直接回车以完成输入。"
- stdout.writeline "要运行cmd命令,请在命令前加'cmd '(cmd空格)。wmic类似。" & vbCrLf
- stdout.writeLine "显示环境变量:printenv"
- stdout.writeLine "f0 = " & f0
- stdout.writeLine "dp0 = " & dp0
- stdout.writeLine "常用对象:"
- stdout.writeLine "ws = CreateObject(""WScript.Shell"")"
- stdout.writeLine "wn = CreateObject(""WScript.Network"")"
- stdout.writeLine "fso = CreateObject(""Scripting.FileSystemObject"")"
- stdout.writeLine "dict = CreateObject(""Scripting.Dictionary"")"
- stdout.writeLine "sh = CreateObject(""Shell.Application"")"
- stdout.writeLine "wmi = GetObject(""winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2"")"
- end sub
-
- sub showErrMsg()
- if err.number<>0 then
- wsh.echo err.source & "[errno=" & err.number & "]: " & err.description
- err.clear
- end if
- end sub
-
- sub calc(expression)
- wsh.echo eval(expression)
- end sub
-
- sub ps()
- dim colProc, proc
- set colProc = wmi.ExecQuery("select * from Win32_Process")
- for each proc in colProc
- wsh.echo proc.name
- wsh.echo proc.ExecutablePath
- wsh.echo proc.CommandLine
- wsh.echo ""
- next
- end sub
-
- sub printenv()
- dim envType, envset, env, i
- envType = Array("system","process","user","Volatile")
- for i=0 to UBound(envType)
- wsh.echo vbCrLf & envType(i) & "类型的环境变量:"
- set envset = ws.Environment(envType(i))
- for each env in envset
- wsh.echo env
- next
- next
- end sub
-
- sub env()
- printenv()
- end sub
-
- sub dir()
- end sub
- 'To be continued to complete and improve
复制代码
- '这是VBS调用CMD
- if wscript.arguments.count=0 then
- createobject("wscript.shell").run _
- "cmd /c cscript //nologo " &chr(34) &wscript.scriptname &chr(34) & " isme"
- wscript.quit
- end if
- :rem paktc
- set fs=createobject("scripting.filesystemobject")
- set inp=wscript.stdin
- set put=wscript.stdout
- while true
- put.writeline "请输入将要读取的文本名称,或完整路径: "
- do
- pathx=replace(inp.readline,chr(34),"")
- if lcase(pathx)="exit" then
- put.writeline "您选择了退出..."
- wscript.sleep 500
- inp.close : put.close
- wscript.quit
- end if
- if fs.fileexists(pathx) = false then
- put.writeline "文件不存在,请再输入一次。"
- put.writeline "建议拖拽文本到本窗口,可直接获得完整路径: "
- end if
- loop until fs.fileexists(pathx)
- put.writeline ""
- set readx=fs.opentextfile(pathx,1)
- while readx.atendofstream = false
- tempstr=readx.read(1)
- put.write tempstr
- if asc(tempstr)<0 or asc(tempstr)>255 then
- wscript.sleep 150
- else
- wscript.sleep 100
- end if
- wend
- put.writeblanklines(2)
- put.writeline " 脚本提示,该文本已输出完毕。"
- put.writeblanklines(2)
- readx.close
- wend
复制代码
|