Knowing when Shell command is complete

Posted on 2007-08-12
Last Modified: 2008-01-09
I am executing a zip and unzip using a shell command in my VB6.0 app.  The command looks like
"  Shell (zipIT & target & backupString)"
The issue is that I can't proceed with my next operation until the unzip (or zip) is complete.

How can I know that the unzip is complete so that I can proceed with my next statement?

I have a rather bogus idea that seems like it might work assuming shell comands are executed in order.
The VB code sequence would be :
1. Execute shell command to create "marker' (Marker.txt) file on the disk.  The name or contents of the file isn't improtant but I would have to know its name  in subsequent steps.

2.  Shell (zipIT & target & backupString)"

3. Execute a shell command to delete the "marker" (Marker.txt) file.

4. VB code that checks for the existense of "Marker.txt" in a timing lopp of approximately 5 seconds.
Something like:
   Dim delaycnt As Integer
   Dim zipFound As Boolean
   zipFound = False
   For delaycnt = 1 To 20   ' total of 5 seconds
        Sleep (250)
        If Dir("Marker.txt") <> "" Then
       else    ' it's gone
              zipFound = false
              Exit For
        End If
  Next delaycnt

When the file dissapears I drop out of the loop and can continue with my next operation because I know that all three shell commands are complete.

Will this work?  There seeems like there must be a more direct way.

what shell command could I execute to crete, and then delete the marker.txt file?

Question by:mlcktmguy
    1 Comment
    LVL 1

    Accepted Solution

    Lots of different ways to wait until the process has ended.  All of them generally use the same approach, shell the process then loop through a check waiting for the process to end.  One example is:

    'vb code Tested on Win XP
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    sub command_click()
          dim Path_Bat as string
          path_Bat="file.exe" 'here path .exe file
          val_exe = Shell(Path_Bat, vbNormalFocus)
          While IsProcessOpen(CLng(val_exe))
           MsgBox "Process Finished"
    end sub

    Function IsProcessOpen(ID_PROCc As Long) As Boolean
    Dim h As Long
    h = OpenProcess(&H1, True, ID_PROCc)
    If h <> 0 Then
      CloseHandle h
      IsProcessOpen= True
      IsProcessOpen= False
    End If
    End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Introduction While answering a recent question ( 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…
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    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

    14 Experts available now in Live!

    Get 1:1 Help Now