Option Explicit
'shell and wait示例
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STATUS_PENDING = &H103
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
'shell and wait
'执行外部命令并等待程序执行完毕再返回控制权
Private Sub Command1_Click()
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变数
pId = Shell("F:\1.bat") ' Shell 传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
DoEvents
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd)
End If
MsgBox "执行完毕 "
End Sub
我也不太明白,不过我有些思路,就是说呢,用一个TIMER来监视一些,你批处理里最后的一个改动,比如说,修改一个文件的名字,在你按按钮的同时,开动时钟控件,然后,MsgBox "清理完成!"
放在timer1里,这样就OK了,不知这样的思路行不行,如果是我,就这样办,也许有更好的办法 思路有了,你可以自己找一下相关的代码,主要是查文件名的代码
对了,忘了,在时钟控件生效前一定要让要改的那个文件名复位,这样不至于出错,我想你也能想到,但我还是想补充一下
'这样就可以满足你的要求:
Private Sub Command1_Click()
Open "C:\test.bat" For Output As #1
Print #1, "@echo off"
Print #1, "echo 测试完毕"
Print #1, "echo. & pause "
Close #1
Dim wshshell
Set wshshell = CreateObject("WScript.Shell")'创建脚本对象
wshshell.run "c:\test.bat"
Set wshshell = Nothing'释放内存
MsgBox "清理完成!"
End Sub
shell函数有一定运行时间,这样修改:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Open "C:\test.bat" For Output As #1
Print #1, "@echo off"
Print #1, "echo 测试完毕"
Print #1, "echo. & pause "
Close #1
Shell "c:\test.bat", vbNormalFocus
Sleep 500 '按实际情况设定时间,单位是毫秒
Form1.SetFocus
MsgBox "清理完成!"
End Sub
这个问题好啊。