Shell 获取 返回(输出文本)
时 间:2018-02-08 10:10:05
作 者:litao ID:37995 城市:上海
摘 要:很多时候,我们调用Shell 执行命令行,需要获取其返回的输出文本,判断执行情况。
下面是一个类模块代码:
IWshRuntimeLibrary 需要引用【Windows Script Host Object Model】
正 文:
很多时候,我们调用Shell 执行命令行,需要获取其返回的输出文本,判断执行情况。
下面是一个类模块代码:
IWshRuntimeLibrary 需要引用【Windows Script Host Object Model】
Option Compare Database '--Shell执行类 Option Explicit Private Const TempDir_ As String = "Temp" '临时文件存储目录 Private Const FileHead As String = "Shl" '临时文件头 Private Sub Class_Terminate() '退出 Dim fso As New Scripting.FileSystemObject fso.DeleteFile Me.TempDir & "\" & FileHead & "*.*" Set fso = Nothing End Sub '====可用方法==== Public Function TempDir() As String TempDir = CurrentProject.Path & "\" & TempDir_ If Dir(TempDir, vbDirectory) = "" Then MkDir TempDir '新建子文件夹 End If End Function Public Function Run(CMD As String) As Long '执行 等待结果 On Error GoTo err1 Dim oShell As IWshRuntimeLibrary.WshShell Set oShell = CreateObject("WScript.Shell") Run = oShell.Run(CMD, 0, True) Set oShell = Nothing Exit Function err1: MsgBox "Run 错误!" & Chr(13) & "CMD:" & CMD & Chr(13) & "ERR:" & err.Description End Function Public Function Exec(CMD As String) As Scripting.Dictionary '执行 获取返回文本的词典 On Error GoTo err1 Dim fso As New Scripting.FileSystemObject Dim TF As TextStream, i As Long Dim TmpP As String, BatPF As String, LogPF As String, Name As String TmpP = Me.TempDir loop1: Name = FileHead & Format(Int((99999 * Rnd) + 1), "00000") BatPF = TmpP & "\" & Name & ".*" If Dir(BatPF) <> "" Then GoTo loop1 BatPF = TmpP & "\" & Name & ".bat" LogPF = TmpP & "\" & Name & ".log" '--创建BAT文件 Set TF = fso.CreateTextFile(BatPF, True) '新建文件 If TF Is Nothing Then GoTo err1 Call TF.Write(CMD) TF.Close '--执行BAT文件 Dim CMDstr As String CMDstr = """" & BatPF & """ >> """ & LogPF & """" Debug.Print CMDstr Me.Run CMDstr '--获取输出 Dim Dic As New Scripting.Dictionary Set TF = fso.OpenTextFile(LogPF, ForReading, False, TristateMixed) '打开文件 If TF Is Nothing Then GoTo err1 '遍历文件 Do Until TF.AtEndOfStream i = TF.Line Dic.Add i, TF.ReadLine 'Debug.Print Dic(i) Loop Set Exec = Dic TF.Close '--删除临时文件 BatPF = TmpP & "\" & Name & ".*" fso.DeleteFile BatPF Set TF = Nothing Set fso = Nothing Exit Function err1: MsgBox "Exec 错误!" & Chr(13) & "CMD:" & CMD & Chr(13) & "ERR:" & err.Description Set TF = Nothing Set fso = Nothing End Function Public Function ExecIsRet(CMD As String, RetStr As String) As Boolean '执行 判断返回是否包含指定字符串 ExecIsRet = False On Error GoTo err1 Dim Dic As Scripting.Dictionary, i As Long, Txt As String Set Dic = Me.Exec(CMD) If Dic Is Nothing Then Exit Function For i = Dic.Count - 1 To 1 Step -1 Txt = Dic.Items(i) If InStr(1, Txt, RetStr) > 0 Then ExecIsRet = True Exit For End If Next Set Dic = Nothing Exit Function err1: MsgBox "ExecIsRet 错误!" & Chr(13) & "CMD:" & CMD & Chr(13) & "ERR:" & err.Description ExecIsRet = False End Function
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access自定义函数命名为中文名...(09.20)
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)