[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] vbs版cmd

  1. 'By zqz0012005@www.bathome.cn
  2. 'Version: 0.3     2008-06-18
  3. On Error Resume Next
  4. host = WScript.FullName
  5. f0 = WScript.ScriptFullName
  6. dp0 = Left( f0, InstrRev(f0,"\") )
  7. if LCase( right(host, len(host)-InStrRev(host,"\")) ) = LCase("wscript.exe") then
  8.     CreateObject("WScript.Shell").run "cmd /d /ccolor f9&title vbs-cmd" &_
  9.         "&cscript //logo """ & f0 & chr(34)
  10.     WScript.Quit
  11. end if
  12. set ws = CreateObject("WScript.Shell")
  13. set wn = CreateObject("WScript.Network")
  14. set fso = CreateObject("Scripting.FileSystemObject")
  15. set dict = CreateObject("Scripting.Dictionary")
  16. set sh = CreateObject("Shell.Application")
  17. set wmi = GetObject("winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2")
  18. 'set colps = wmi.instancesOf("win32_process")
  19. 'set colps = GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
  20. set stdin = WScript.stdin
  21. set stdout = WScript.stdout
  22. stdout.writeline "可以直接运行vbs语句,要运行cmd命令,请在命令前加'cmd '(cmd空格)"
  23. do
  24.     stdout.writeBlankLines(1)
  25.     stdout.write ws.CurrentDirectory & vbCrLf & "# "
  26.     s=Trim(stdin.readLine)
  27.     Select Case 0
  28.         Case StrComp(s,"exit",1) wsh.quit
  29.         Case StrComp(left(s,2),"cd",1)
  30.             if mid(s,3)="" then
  31.                 wsh.echo ws.CurrentDirectory
  32.             elseif mid(s,3,1)=" " then
  33.                 ws.CurrentDirectory = LTrim(mid(s,3))
  34.             else
  35.                 execStatementBlock
  36.             end if
  37.         Case StrComp(left(s,4),"cmd ",1)
  38.             set oexec=ws.exec( "cmd /d /q /c" & LTrim(mid(s,4)) & "&exit" )
  39.             'oexec.stdin.writeLine vbCrLf & "exit"
  40.             stdout.write oexec.stdout.readall & oexec.stderr.readall
  41.             set oexec=nothing
  42.         Case StrComp(left(s,5),"wmic ",1)
  43.             set oexec=ws.exec("cmd /d /q /c echo exit|" & s)
  44.             'oexec.stdin.writeLine vbCrLf & "exit"
  45.             stdout.write oexec.stdout.readall & oexec.stderr.readall
  46.             set oexec=nothing
  47.         Case StrComp(s,"help",1) call help()
  48.         Case StrComp(s,"") 'do nothing
  49.         Case Else
  50.             LastCode = execStatementBlock()
  51.         'Case Else execute s
  52.         'Case Else wsh.echo "[" & s & "]"
  53.     End Select
  54.     showErrMsg
  55. loop
  56. function execStatementBlock()
  57. dim i, t
  58. ReDim a(0)
  59. a(0) = s
  60. stdout.write "More? "
  61. t = Trim(stdin.readLine)
  62. do until t=""
  63. i = i + 1
  64. ReDim Preserve a(i)
  65. a(i) = t
  66. stdout.write "More? "
  67. t = Trim(stdin.readLine)
  68. loop
  69. execute Join(a, ":")
  70. execStatementBlock = Join(a, vbCrLf)
  71. end function
  72. sub help()
  73. stdout.writeline vbCrLf & "可以直接运行vbs语句,在More? 提示符后直接回车以完成输入。"
  74. stdout.writeline "要运行cmd命令,请在命令前加'cmd '(cmd空格)。wmic类似。" & vbCrLf
  75. stdout.writeLine "显示环境变量:printenv"
  76. stdout.writeLine "f0 = " & f0
  77. stdout.writeLine "dp0 = " & dp0
  78. stdout.writeLine "常用对象:"
  79. stdout.writeLine "ws = CreateObject(""WScript.Shell"")"
  80. stdout.writeLine "wn = CreateObject(""WScript.Network"")"
  81. stdout.writeLine "fso = CreateObject(""Scripting.FileSystemObject"")"
  82. stdout.writeLine "dict = CreateObject(""Scripting.Dictionary"")"
  83. stdout.writeLine "sh = CreateObject(""Shell.Application"")"
  84. stdout.writeLine "wmi = GetObject(""winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2"")"
  85. end sub
  86. sub showErrMsg()
  87. if err.number<>0 then
  88.     wsh.echo err.source & "[errno=" & err.number & "]: " & err.description
  89.     err.clear
  90. end if
  91. end sub
  92. sub calc(expression)
  93. wsh.echo eval(expression)
  94. end sub
  95. sub ps()
  96. dim colProc, proc
  97. set colProc = wmi.ExecQuery("select * from Win32_Process")
  98. for each proc in colProc
  99.     wsh.echo proc.name
  100.     wsh.echo proc.ExecutablePath
  101.     wsh.echo proc.CommandLine
  102.     wsh.echo ""
  103. next
  104. end sub
  105. sub printenv()
  106. dim envType, envset, env, i
  107. envType = Array("system","process","user","Volatile")
  108. for i=0 to UBound(envType)
  109.   wsh.echo vbCrLf & envType(i) & "类型的环境变量:"
  110.   set envset = ws.Environment(envType(i))
  111.   for each env in envset
  112.     wsh.echo env
  113.   next
  114. next
  115. end sub
  116. sub env()
  117.     printenv()
  118. end sub
  119. sub dir()
  120. end sub
  121. 'To be continued to complete and improve
复制代码
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

回复 4楼 的帖子

就是这个功能啊,核心都是vbs+cscript.exe

cmd命令是次要功能,因为有些命令无法获取标准流 或 用vbs函数代替进行模拟,
而且cmd命令失去交互能力了,被exec所劫持。
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

TOP

有想过 cmd版vbs(就cscript模式吧)
可以允许 将vbs的函数像命令一样输入。

TOP

的确比较难搞,我花了很多时间才搞定运行不带参数的 comp 之类的命令,但是对于不带参数的 wmic 之类的命令就没办法了,不知道有没有什么好的办法?

TOP

谢谢版主,可以说明一下用途与一般cmd之差异?或给使用范例 VBS不熟?
# help

可以直接运行vbs语句,在More? 提示符后直接回车以完成输入。
要运行cmd命令,请在命令前加'cmd '(cmd空格)。wmic类似。

显示环境变量:printenv
f0 = D:\scripts\TEST\0000\20100621A\vbs-cmd\vbs-cmd.vbs
dp0 = D:\scripts\TEST\0000\20100621A\vbs-cmd\
常用对象:
ws = CreateObject("WScript.Shell")
wn = CreateObject("WScript.Network")
fso = CreateObject("Scripting.FileSystemObject")
dict = CreateObject("Scripting.Dictionary")
sh = CreateObject("Shell.Application")
wmi = GetObject("winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2")

TOP

返回列表