How can I execute a command and wait for it to return?

Posted on 2006-05-19
Last Modified: 2011-09-20
I'm looking for something real basic that will allow me  to execute a windows command inside my VB program and wait for it to return. I'm currently using something like:

cmdStr = "dir /s /b>files.txt"

My problem is that when I try to open the files.txt file for reading, the actual dir command hasn't finished. Is there a way to get Shell to block until the command running is completed? Thanks very much for any help!
Question by:nub99
    LVL 3

    Expert Comment

    You could use a loop after that. Something like:

    Variable = Dir("Files.txt")
    Do While Variable = ""

    Or something like that.

    LVL 8

    Expert Comment

    If you do that, I would suggest a doevents inside of the loop, to make sure it does not lock up completely.
    LVL 2

    Expert Comment

    LVL 26

    Expert Comment

    Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run environ("COMSPEC") & " /c dir /s /b>files.txt",0, true
    Set wshShell = Nothing
    LVL 15

    Accepted Solution

    From VBNet (

    Option Explicit

    ' Copyright ©1996-2006 VBnet, Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ' Distribution: You can freely use this code in your own
    '               applications, but you may not reproduce
    '               or publish this code on any web site,
    '               online service, or distribute as source
    '               on any media without express permission.

    Private Const WAIT_INFINITE = -1&
    Private Const SYNCHRONIZE = &H100000

    Private Declare Function OpenProcess Lib "kernel32" _
      (ByVal dwDesiredAccess As Long, _
       ByVal bInheritHandle As Long, _
       ByVal dwProcessId As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" _
      (ByVal hHandle As Long, _
       ByVal dwMilliseconds As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long) As Long  

    Private Sub Command1_Click()

       Dim hProcess As Long
       Dim taskId As Long
       Dim cmdline As String
       cmdline = "notepad.exe"
       taskId = Shell(cmdline, vbNormalFocus)
       hProcess = OpenProcess(SYNCHRONIZE, True, taskId)
       Call WaitForSingleObject(hProcess, WAIT_INFINITE)
       CloseHandle hProcess
       MsgBox "The shelled app has ended."

    End Sub

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now