标题: [特效代码] 批处理与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 编辑
- :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
复制代码
作者: pcl_test 时间: 2016-7-15 14:25
本帖最后由 pcl_test 于 2017-6-21 21:04 编辑
以下代码均保存为bat文件运行
方式一,传递一个数据- ' 2>nul 3>nul&cls&@echo off
- '&for /f %%a in ('echo;123^|cscript -nologo -e:vbscript "%~fs0"') do set str=%%a
- '&echo;%str%
- '&pause&exit
-
- s= replace(WSH.StdIn.ReadLine,"2","test")
- WSH.Echo s
复制代码
方式二,传递一个数据- ' 2>nul 3>nul&cls&@echo off
- '&for /f %%a in ('cscript -nologo -e:vbscript "%~fs0" "123"') do set str=%%a
- '&echo;%str%
- '&pause&exit
-
- s= replace(WSH.Arguments(0),"2","test")
- WSH.Echo s
复制代码
方式三,传递多个数据- ' 2>nul 3>nul&cls&@echo off
- '&for /f %%a in ('(for /l %%i in (1 1 5^) do @echo;%%i^)^|cscript -nologo -e:vbscript "%~fs0"') do echo;%%a
- '&pause&exit
-
- s= replace(WSH.StdIn.ReadAll,"3","test")
- WSH.Echo s
复制代码
方式四,传递多个数据- ' 2>nul 3>nul&cls&@echo off
- '&for /f %%a in ('(for /l %%i in (1 1 5^) do @echo;%%i^)^|cscript -nologo -e:vbscript "%~fs0"') do echo;%%a
- '&pause&exit
-
- Do While WSH.StdIn.AtEndOfStream <> true
- s=s&replace(WSH.StdIn.ReadLine,"3","test")&vbCrLf
- Loop
- WSH.Echo s
复制代码
方式五,传递多个数据- ' 2>nul 3>nul&cls&@echo off
- '&for /f %%a in ('cscript -nologo -e:vbscript "%~fs0" 1 2 3 4 5') do echo;%%a
- '&pause&exit
-
- for i=0 to WSH.Arguments.Count-1
- s=s&replace(WSH.Arguments(i),"3","test")&vbCrLf
- Next
- WSH.Echo s
复制代码
方式六- @echo off
- for /f "delims=" %%a in ('mshta VBScript:Execute("CreateObject(""Scripting.Filesystemobject"").GetStandardStream(1).Write(""当前时间是""&now)"^)(close^)') do echo;%%a
- pause
复制代码
传递输出文本内容方式,配合WSH.StdIn.ReadLine/WSH.StdIn.ReadAll、WSH.Echo- '&cls&type "a.txt"|cscript -nologo -e:vbscript "%~fs0">"b.txt"&pause
- WSH.echo WSH.StdIn.ReadLine&vbCrLf&"举个栗子"
复制代码
- '&cls&cscript -nologo -e:vbscript "%~fs0"<"a.txt">"b.txt"&pause
- WSH.echo WSH.StdIn.ReadAll&vbCrLf&"举个栗子"
复制代码
另一种混合方式,不要求在cmd控制台窗口回显,弹出提示框方式显示- '&cls&@echo off
- '&cls&echo;123|cscript -nologo -e:vbscript "%~fs0"
- '&cls&pause&exit
- msgbox replace(WSH.StdIn.ReadAll,"2","test")
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |