Board logo

标题: [特效代码] 批处理与vbs混编如何互相传值/传递参数? [打印本页]

作者: zhanglei1371    时间: 2013-12-2 21:56     标题: 批处理与vbs混编如何互相传值/传递参数?

本帖最后由 pcl_test 于 2016-7-17 22:48 编辑

看到这个帖子:
http://bbs.bathome.net/redirect. ... 9641&ptid=27060
里面有如下的方法:第一行就是引用jscript到bat中,我想问的是,加入用同样的方式,如何将vbs也引入其中去?
当然我知道可以cscript 后直接可以接vbs文件名,但是可否也以同样的~f0的方式自我执行vbs?
作者: apang    时间: 2013-12-2 22:11

貌似 vbs要生成临时文件才行
作者: zhanglei1371    时间: 2013-12-3 00:12

回复 2# apang


    就算生成临时文件,我最后的提问,如何进行遍历操作 vbs呢?或者能否前面是bat的dir,后面是运行vbs临时文件,整个主过程还是通过双击bat来完成?
作者: loquat    时间: 2013-12-14 00:33

原来楼主批也用得这么牛,佩服。。。不同的领域看到楼主了。。。
作者: zhanglei1371    时间: 2013-12-14 07:00

回复 4# loquat


    惭愧惭愧!到现在还只是入门水平而已,多学几门语言,开拓下眼界
作者: loquat    时间: 2013-12-15 01:52

本帖最后由 loquat 于 2013-12-15 01:56 编辑
  1. :On Error Resume Next
  2. Sub bat
  3. echo off & cls
  4. echo Batching_codez_here_following_vbs_rules & pause
  5. start wscript -e:vbs "%~f0"
  6. Exit Sub
  7. End Sub
  8. MsgBox "This is vbs"
复制代码
  1. ::' 带参数的版本
  2. :On Error Resume Next
  3. :Sub bat
  4. echo off & cls
  5. echo Batching_codez_here_following_vbs_rules & pause >nul
  6. echo '>nul & start "" wscript //e:vbscript "%~f0" %*
  7. Exit Sub : End Sub
  8. MsgBox "This is vbs"
  9. for each i in wscript.arguments
  10. wscript.echo i
  11. next
复制代码
  1. ::' 另一种方案的混合
  2. ::' 源网址http://hi.baidu.com/paktc/item/27c33e3b1cf831be603aff97
  3. ' 2>nul &echo off &cls &fsutil file setshortname "%~nx0" "%~n0.vbs"
  4. :'loop
  5. ' 2>nul &set /p inp="你可以输入两个小数,本脚本尝试作乘法运算: "
  6. ' 2>nul &if "%inp%" == "exit" (exit)
  7. ' 2>nul &cscript //nologo "%~n0.vbs" %inp%
  8. ' 2>nul &pause &cls &goto :'loop
  9. set x=wscript.arguments
  10. call count(x(0),x(1))
  11. sub count(a,b)
  12. wscript.echo a*b
  13. wscript.quit
  14. end sub
复制代码
  1. 'vbs版cmd
  2. '可执行bat,也可以执行vbs
  3. 'By zqz0012005@www.bathome.cn
  4. 'Version: 0.3     2008-06-18
  5. On Error Resume Next
  6. host = WScript.FullName
  7. f0 = WScript.ScriptFullName
  8. dp0 = Left( f0, InstrRev(f0,"\") )
  9. if LCase( right(host, len(host)-InStrRev(host,"\")) ) = LCase("wscript.exe") then
  10.     CreateObject("WScript.Shell").run "cmd /d /ccolor f9&title vbs-cmd" &_
  11.         "&cscript //logo """ & f0 & chr(34)
  12.     WScript.Quit
  13. end if
  14. set ws = CreateObject("WScript.Shell")
  15. set wn = CreateObject("WScript.Network")
  16. set fso = CreateObject("Scripting.FileSystemObject")
  17. set dict = CreateObject("Scripting.Dictionary")
  18. set sh = CreateObject("Shell.Application")
  19. set wmi = GetObject("winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2")
  20. 'set colps = wmi.instancesOf("win32_process")
  21. 'set colps = GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
  22. set stdin = WScript.stdin
  23. set stdout = WScript.stdout
  24. stdout.writeline "可以直接运行vbs语句,要运行cmd命令,请在命令前加'cmd '(cmd空格)"   '::现版本没有这条限制
  25. do
  26.     stdout.writeBlankLines(1)
  27.     stdout.write ws.CurrentDirectory & vbCrLf & "# "
  28.     s=Trim(stdin.readLine)
  29.     Select Case 0
  30.         Case StrComp(s,"exit",1) wsh.quit
  31.         Case StrComp(left(s,2),"cd",1)
  32.             if mid(s,3)="" then
  33.                 wsh.echo ws.CurrentDirectory
  34.             elseif mid(s,3,1)=" " then
  35.                 ws.CurrentDirectory = LTrim(mid(s,3))
  36.             else
  37.                 execStatementBlock
  38.             end if
  39.         Case StrComp(left(s,4),"cmd ",1)
  40.             set oexec=ws.exec( "cmd /d /q /c" & LTrim(mid(s,4)) & "&exit" )
  41.             'oexec.stdin.writeLine vbCrLf & "exit"
  42.             stdout.write oexec.stdout.readall & oexec.stderr.readall
  43.             set oexec=nothing
  44.         Case StrComp(left(s,5),"wmic ",1)
  45.             set oexec=ws.exec("cmd /d /q /c echo exit|" & s)
  46.             'oexec.stdin.writeLine vbCrLf & "exit"
  47.             stdout.write oexec.stdout.readall & oexec.stderr.readall
  48.             set oexec=nothing
  49.         Case StrComp(s,"help",1) call help()
  50.         Case StrComp(s,"") 'do nothing
  51.         Case Else
  52.             LastCode = execStatementBlock()
  53.         'Case Else execute s
  54.         'Case Else wsh.echo "[" & s & "]"
  55.     End Select
  56.     showErrMsg
  57. loop
  58. function execStatementBlock()
  59. dim i, t
  60. ReDim a(0)
  61. a(0) = s
  62. stdout.write "More? "
  63. t = Trim(stdin.readLine)
  64. do until t=""
  65. i = i + 1
  66. ReDim Preserve a(i)
  67. a(i) = t
  68. stdout.write "More? "
  69. t = Trim(stdin.readLine)
  70. loop
  71. execute Join(a, ":")
  72. execStatementBlock = Join(a, vbCrLf)
  73. end function
  74. sub help()
  75. stdout.writeline vbCrLf & "可以直接运行vbs语句,在More? 提示符后直接回车以完成输入。"
  76. stdout.writeline "要运行cmd命令,请在命令前加'cmd '(cmd空格)。wmic类似。" & vbCrLf
  77. stdout.writeLine "显示环境变量:printenv"
  78. stdout.writeLine "f0 = " & f0
  79. stdout.writeLine "dp0 = " & dp0
  80. stdout.writeLine "常用对象:"
  81. stdout.writeLine "ws = CreateObject(""WScript.Shell"")"
  82. stdout.writeLine "wn = CreateObject(""WScript.Network"")"
  83. stdout.writeLine "fso = CreateObject(""Scripting.FileSystemObject"")"
  84. stdout.writeLine "dict = CreateObject(""Scripting.Dictionary"")"
  85. stdout.writeLine "sh = CreateObject(""Shell.Application"")"
  86. stdout.writeLine "wmi = GetObject(""winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2"")"
  87. end sub
  88. sub showErrMsg()
  89. if err.number<>0 then
  90.     wsh.echo err.source & "[errno=" & err.number & "]: " & err.description
  91.     err.clear
  92. end if
  93. end sub
  94. sub calc(expression)
  95. wsh.echo eval(expression)
  96. end sub
  97. sub ps()
  98. dim colProc, proc
  99. set colProc = wmi.ExecQuery("select * from Win32_Process")
  100. for each proc in colProc
  101.     wsh.echo proc.name
  102.     wsh.echo proc.ExecutablePath
  103.     wsh.echo proc.CommandLine
  104.     wsh.echo ""
  105. next
  106. end sub
  107. sub printenv()
  108. dim envType, envset, env, i
  109. envType = Array("system","process","user","Volatile")
  110. for i=0 to UBound(envType)
  111.   wsh.echo vbCrLf & envType(i) & "类型的环境变量:"
  112.   set envset = ws.Environment(envType(i))
  113.   for each env in envset
  114.     wsh.echo env
  115.   next
  116. next
  117. end sub
  118. sub env()
  119.     printenv()
  120. end sub
  121. sub dir()
  122. end sub
  123. 'To be continued to complete and improve
复制代码
  1. '这是VBS调用CMD
  2. if wscript.arguments.count=0 then
  3.    createobject("wscript.shell").run _
  4.    "cmd /c cscript //nologo " &chr(34) &wscript.scriptname &chr(34) & " isme"
  5.    wscript.quit
  6. end if
  7. :rem paktc
  8. set fs=createobject("scripting.filesystemobject")
  9. set inp=wscript.stdin
  10. set put=wscript.stdout
  11. while true
  12. put.writeline "请输入将要读取的文本名称,或完整路径: "
  13. do
  14.     pathx=replace(inp.readline,chr(34),"")
  15.     if lcase(pathx)="exit" then
  16.       put.writeline "您选择了退出..."
  17.       wscript.sleep 500
  18.       inp.close : put.close
  19.       wscript.quit
  20.     end if
  21.     if fs.fileexists(pathx) = false then
  22.       put.writeline "文件不存在,请再输入一次。"
  23.       put.writeline "建议拖拽文本到本窗口,可直接获得完整路径: "
  24.     end if
  25. loop until fs.fileexists(pathx)
  26. put.writeline ""
  27. set readx=fs.opentextfile(pathx,1)
  28. while readx.atendofstream = false
  29.     tempstr=readx.read(1)
  30.     put.write tempstr
  31.     if asc(tempstr)<0 or asc(tempstr)>255 then
  32.       wscript.sleep 150
  33.     else
  34.       wscript.sleep 100
  35.     end if
  36. wend
  37. put.writeblanklines(2)
  38. put.writeline "    脚本提示,该文本已输出完毕。"
  39. put.writeblanklines(2)
  40. readx.close
  41. wend
复制代码

作者: pcl_test    时间: 2016-7-15 14:25

本帖最后由 pcl_test 于 2017-6-21 21:04 编辑

以下代码均保存为bat文件运行
方式一,传递一个数据
  1. ' 2>nul 3>nul&cls&@echo off
  2. '&for /f %%a in ('echo;123^|cscript -nologo -e:vbscript "%~fs0"') do set str=%%a
  3. '&echo;%str%
  4. '&pause&exit
  5. s= replace(WSH.StdIn.ReadLine,"2","test")
  6. WSH.Echo s
复制代码
方式二,传递一个数据
  1. ' 2>nul 3>nul&cls&@echo off
  2. '&for /f %%a in ('cscript -nologo -e:vbscript "%~fs0" "123"') do set str=%%a
  3. '&echo;%str%
  4. '&pause&exit
  5. s= replace(WSH.Arguments(0),"2","test")
  6. WSH.Echo s
复制代码
方式三,传递多个数据
  1. ' 2>nul 3>nul&cls&@echo off
  2. '&for /f %%a in ('(for /l %%i in (1 1 5^) do @echo;%%i^)^|cscript -nologo -e:vbscript "%~fs0"') do echo;%%a
  3. '&pause&exit
  4. s= replace(WSH.StdIn.ReadAll,"3","test")
  5. WSH.Echo s
复制代码
方式四,传递多个数据
  1. ' 2>nul 3>nul&cls&@echo off
  2. '&for /f %%a in ('(for /l %%i in (1 1 5^) do @echo;%%i^)^|cscript -nologo -e:vbscript "%~fs0"') do echo;%%a
  3. '&pause&exit
  4. Do While WSH.StdIn.AtEndOfStream <> true
  5.     s=s&replace(WSH.StdIn.ReadLine,"3","test")&vbCrLf
  6. Loop
  7. WSH.Echo s
复制代码
方式五,传递多个数据
  1. ' 2>nul 3>nul&cls&@echo off
  2. '&for /f %%a in ('cscript -nologo -e:vbscript "%~fs0" 1 2 3 4 5') do echo;%%a
  3. '&pause&exit
  4. for i=0 to WSH.Arguments.Count-1
  5.     s=s&replace(WSH.Arguments(i),"3","test")&vbCrLf
  6. Next
  7. WSH.Echo s
复制代码
方式六
  1. @echo off
  2. for /f "delims=" %%a in ('mshta VBScript:Execute("CreateObject(""Scripting.Filesystemobject"").GetStandardStream(1).Write(""当前时间是""&now)"^)(close^)') do echo;%%a
  3. pause
复制代码
传递输出文本内容方式,配合WSH.StdIn.ReadLine/WSH.StdIn.ReadAll、WSH.Echo
  1. '&cls&type "a.txt"|cscript -nologo -e:vbscript "%~fs0">"b.txt"&pause
  2. WSH.echo WSH.StdIn.ReadLine&vbCrLf&"举个栗子"
复制代码
  1. '&cls&cscript -nologo -e:vbscript "%~fs0"<"a.txt">"b.txt"&pause
  2. WSH.echo WSH.StdIn.ReadAll&vbCrLf&"举个栗子"
复制代码
另一种混合方式,不要求在cmd控制台窗口回显,弹出提示框方式显示
  1. '&cls&@echo off
  2. '&cls&echo;123|cscript -nologo -e:vbscript "%~fs0"
  3. '&cls&pause&exit
  4. msgbox replace(WSH.StdIn.ReadAll,"2","test")
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2