kurukuru-papaのブログ

主に、ソフトウェア開発に関連したメモを書き溜めたいと思います。

MS-DOSコマンドを実行し、結果を取得する

' DOSコマンドを実行する
' 2008/12/22 新規作成
Public Sub U_Exec(cmd As String, resultArr As Variant)
    Dim wsh
    Dim exec
    Dim resultStr
    
    ' 前準備
    Set wsh = CreateObject("WScript.Shell")
    Set exec = wsh.exec("%ComSpec% /c " & cmd)
    
    ' コマンド実行
    Do While exec.Status = 0
        DoEvents
    Loop
    
    ' 標準出力を取得する
    resultStr = exec.StdOut.ReadAll
    resultArr = Split(resultStr, vbCrLf)
    
    ' 後始末
    Set exec = Nothing
    Set wsh = Nothing
End Sub