[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Knowing when Shell command is complete

Posted on 2007-08-12
1
Medium Priority
?
225 Views
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?


0
Comment
Question by:mlcktmguy
1 Comment
 
LVL 1

Accepted Solution

by:
epackwood earned 1000 total points
ID: 19680279
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))
                 DoEvents
       Wend
       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
Else
  IsProcessOpen= False
End If
End Function
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

872 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