Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2006-05-19
Medium Priority
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

Expert Comment

ID: 16719524
You could use a loop after that. Something like:

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

Or something like that.


Expert Comment

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

Expert Comment

ID: 16719632
LVL 26

Expert Comment

ID: 16719850
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

cquinn earned 1000 total points
ID: 16720234
From VBNet (http://vbnet.mvps.org/)

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

581 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