vbscript VBA - 有用的功能(与CMD交互)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript VBA - 有用的功能(与CMD交互)相关的知识,希望对你有一定的参考价值。

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' First method reads STDOUT directly
' Uses exec, unavoidable cmd blip

    Set WSH = New WshShell
    Set oExec = WSH.Exec("cmd /C findstr " & spattern & Space(1) & File_in)
    Set oOutput = oExec.StdOut
    
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend
    Find_Line = s
    
    
    
    
    
    
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' First method reads STDOUT through a temporary file and deletes it
' Uses run, avoidable cmd blip


    Dim cmdCommand As String
    Dim strOutput
    
    'cmdCommand = "cmd.exe /C findstr " & spattern & Space(1) & File_in & " > C:\out.txt"
    'MsgBox (cmdCommand)
    
    With CreateObject("WScript.Shell")
        '.Run "cmd /C findstr " & spattern & Space(1) & File_in & " > C:\Users\p.doulgeridis\Desktop\out.txt", 0, True
        .Run "cmd /C findstr " & spattern & Space(1) & File_in & " > " & TempDir, 0, True
    End With
    
    With CreateObject("Scripting.FileSystemObject")
        strOutput = .OpenTextFile("C:\Users\p.doulgeridis\Desktop\out.txt").ReadAll()
        .DeleteFile "C:\Users\p.doulgeridis\Desktop\out.txt"
    End With
    
    Find_Line_nocmd = strOutput
Public Function Find_Line_nocmd(spattern As String, File_in As String) As String
    
    
    ' Function : Find_Line_nocmd
    ' Work     : Check a specific file for a line and print it, no cmd window
    ' Called as: Find_Line_nocmd(string, /path/to/file)
    ' Called at: Main Form Find Line Button
    ' Input    : string to be checked, file_in (path)
    ' Notes    : This is the one actually used in main form.
    
    ' Construct command line
    Dim cmdCommand As String
    Dim strOutput
    
    'cmdCommand = "cmd.exe /C findstr " & spattern & Space(1) & File_in & " > C:\out.txt"
    'MsgBox (cmdCommand)
    
    With CreateObject("WScript.Shell")
        '.Run "cmd /C findstr " & spattern & Space(1) & File_in & " > C:\Users\p.doulgeridis\Desktop\out.txt", 0, True
        .Run "cmd /C findstr " & spattern & Space(1) & File_in & " > " & TempDir, 0, True
    End With
    
    With CreateObject("Scripting.FileSystemObject")
        strOutput = .OpenTextFile("C:\Users\p.doulgeridis\Desktop\out.txt").ReadAll()
        .DeleteFile "C:\Users\p.doulgeridis\Desktop\out.txt"
    End With
    
    Find_Line_nocmd = strOutput
End Function
Public Function Find_Line(spattern As String, File_in As String) As String
    ' This version blips cmd
    
    ' Function : Find_Line
    ' Work     : Check a specific file for a line and print it
    ' Called as: Find_Line(string, /path/to/file)
    ' Called at: Main Form Find Line Button
    ' Input    : string to be checked, file_in (path)
    
    
    Dim WSH As WshShell
    Dim oExec As Object
    Dim oOutput As Object
    
    Set WSH = New WshShell
    Set oExec = WSH.Exec("cmd /C findstr " & spattern & Space(1) & File_in)
    Set oOutput = oExec.StdOut
    
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend
    Find_Line = s
    
    ' This does not blip cmd
    'Set oExec = WSH.Run("cmd /C findstr " & spattern & Space(1) & File_in & " > C:\Users\p.doulgeridis\Desktop\out.txt", 0, True
    
    
    
    
    
    
    'Dim resp As String
    'resp = redirect(StdOut)
    'S = resp
    'S = StdOut.ReadLine
    
    'Do Until StdOut.AtEndOfStream
    '    S = S & vbCrLf & StdOut.ReadLine
    'Loop
    'MsgBox (S)
    'Find_Line = S
End Function

以上是关于vbscript VBA - 有用的功能(与CMD交互)的主要内容,如果未能解决你的问题,请参考以下文章

vbscript REGEX功能VBA

vbscript Outlook功能代码段(VBA)

vbscript VBA最大和最小功能

将 Staad 与 VBS 一起使用(将 VBA 文档转换为 VBscript)

vbscript MS Access VBA查询功能将CDec()应用于数字字段(有助于避免舍入错误)

vbscript VBScript或VBA数组函数