Friday, November 29, 2019

 1.Basic

- Run: wait to command finish and return exit code only

- Exec: get output stream of command


Option Explicit




Sub test_compare_getReturnCode()
    Dim cmd As String
    cmd = "fc /lb2 /b C:\Users\NinhLD\Downloads\vba\cmp\as.exe C:\Users\NinhLD\Downloads\vba\cmp\ash.exe > NUL"
    
    
    Dim ret As Integer
    ret = ShellRunGetReturnCode(cmd)
    Select Case ret
        Case 0
            Debug.Print "two File are the same"
        Case Else
            Debug.Print "two File are not the same"
    End Select
End Sub

Sub test_compare_getOutput()
    Dim cmd As String
    cmd = "fc /b C:\Users\NinhLD\Downloads\vba\cmp\excel_VBA.txt C:\Users\NinhLD\Downloads\vba\cmp\excel_VBA2.txt"

    Debug.Print ShellRunGetOutput(cmd)
End Sub










'---API start---
Public Function ShellRunGetReturnCode(ByVal sCmd As String) As Integer
    ShellRunGetReturnCode = -1
    
    sCmd = "cmd /c " & sCmd

    Dim oShell As Object
    Set oShell = CreateObject("WScript.Shell")
    
    ShellRunGetReturnCode = oShell.Run(sCmd, 0, True) 'set 0 to hiden window, 1 to show window
End Function


Public Function ShellRunGetOutput(sCmd As String) As String

    'Run a shell command, returning the output as a string

    Dim oShell As Object
    Set oShell = CreateObject("WScript.Shell")

    'run command
    Dim oExec As Object
    Dim oOutput As Object
    Set oExec = oShell.Exec(sCmd)
    Set oOutput = oExec.StdOut

    'handle the results as they are written to and read from the StdOut object
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend

    ShellRunGetOutput = s

End Function

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -