How do i check if cmd window is opened?

P

Philippe

In have an Excel workbook that creates a command window, and uses the
compact command to compress a folder, and I want to loop the code so as to
wait until the cmd window has closed: How do i check if cmd window is
opened?

Thanks in advance


Philippe

philippeoget at hotmail dot com
http://www.geocities.com/philippeoget/a2z/
 
H

Helmut Weber

Hi Philippe,

there are many ways, truly professional ones
and lots of workarounds.
For a professional solution see, in German language:
http://vb-tec.de/xshell.htm

One workaraound could be
checking whether a command shell is in the task-list,
see help for "task-object".

Another workaraound:
You may create a temporary file before executing
"compact", delete it after execution of "compact",
and check, whether the temporary file has vanished
before continuing execution. You might like to wait
a second or so before checking file existence and
before repeating the check whether the file still exists.

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

GPDynamics

Philippe,

Here is code I use recursively to run a series of DOS Commands in a Command
Prompt Window:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2004 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
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub AllSub()
Dim q As String
Dim r As Integer
Dim s As String
Dim x As String
Dim dStarted As Date
Dim dEnded As Date
Dim lDiff As Long
Dim hProcess As Long
Dim taskId As Long
Dim cmdline As String

Sheets("PleaseWait").Visible = True
Application.Goto Reference:="PleaseWait"
Application.ScreenUpdating = False

r = 1
x = Range("LoopHold") ' This is variable that identifies last button
used; maximum counter
On Error Resume Next
Worksheets("macros").Range("GroupTime").ClearContents
Do Until r = x + 1
s = "Title" & r & "TimeHold"
Worksheets("macros").Range("Counter").Value = r
q = Range("ShellHold") ' This captures all switches chosen by user
to pass variables to batch file
dStarted = Now()
If Range("AllTitleHold") <> 0 Then taskId = Shell(q) ',
vbMinimizedNoFocus)

Application.Calculate
hProcess = OpenProcess(SYNCHRONIZE, True, taskId)
Call WaitForSingleObject(hProcess, WAIT_INFINITE)
CloseHandle hProcess
r = r + 1
dEnded = Now()
lDiff = DateDiff("S", dStarted, dEnded)
Application.Calculate
Range(s).Value = lDiff
Loop
Worksheets("macros").Range("AllTitleHold1").Value = ""
Call HideSheets
ThisWorkbook.Activate
End Sub

I add a timer to each process so I know how long each takes and how long the
whole process takes so that I can schedule accordingly.

I hope this helps.

Michael
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top