LoginSignup
7
8

More than 5 years have passed since last update.

VBAでDOSコマンド

Last updated at Posted at 2014-08-13

概要

VBAでDOSコマンドを使う方法

内容

  • run() は非同期実行の為、exec()で同期実行します。
  • 取得情報が長い場合は、一度wExec.StdOut.ReadAllを実行しないとフリーズしました。

ソース

DOSコマンド実行関数

Function getCmd(sCmd As String) As String()
    Dim WSH, wExec, Result As String
    Set WSH = CreateObject("WScript.Shell")
    Set wExec = WSH.Exec("%ComSpec% /c " & sCmd)

    tmp_str = ""
     Do
        tmp_str = tmp_str & wExec.StdOut.ReadAll
        DoEvents
     Loop While wExec.Status = 0

    getCmd = Split(tmp_str, vbCrLf)

    Set wExec = Nothing
    Set WSH = Nothing
End Function

Sub main()
    dirs = getCmd("echo hello")
End Sub

DOSコマンド

これを実行する為に作成しました。

コマンド 内容
dir /b/s/a-d 再帰的にファイル取得※

※dirコマンドの引数

引数 内容
/b ファイル名のみ表示
/a-d フィルタ ( -d でフォルダ除外 )
/s 再帰的

参考

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8