Wie mittels Shell-Anweisung eine Batch ausführen und auf Ende warten

Hallo,
ich möchte über eine Shell Anweisung eine Batch ausführen und warten bis diese fertig ist, erst danach soll die nächste zeile in VB ausgeführt werden.
geht das ?

mfg jonny

Hi Joe,

ich möchte über eine Shell Anweisung eine Batch ausführen und
warten bis diese fertig ist, erst danach soll die nächste
zeile in VB ausgeführt werden.
geht das ?

Ja. Du kannst in VB ein Konsolenfenster öffnen. Bei AVB findest Du den Code bei den Tipps.
Gruß, Rainer

Hallo Jonny,
nunja mit dem Befehl Shell geht des net. Aber man nen bissl tricksen :wink:
Folgende Functionen sollten dir weiterhelfen :smile:

Option explicit

Private Declare Function CloseHandle Lib "kernel32" (
 ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (
 ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" ( 
 ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long,
 ByVal dwProcessId As Long) As Long

Public Function ShellX( 
 ByVal PathName As String,
 Optional ByVal WindowStyle As VbAppWinStyle = vbMinimizedFocus,
 Optional ByVal Events As Boolean = True
 ) As Long
 Const STILL_ACTIVE = &H103&
 Const PROCESS_QUERY_INFORMATION = &H400&
 Dim ProcId As Long
 Dim ProcHnd As Long
 ProcId = Shell(PathName, WindowStyle)
 ProcHnd = OpenProcess(PROCESS_QUERY_INFORMATION, True, ProcId)
 Do
 If Events Then DoEvents
 GetExitCodeProcess ProcHnd, ShellX
 Loop While ShellX = STILL_ACTIVE
 CloseHandle ProcHnd
End Function

'Ein Aufruf könnte wiefolgt ausschauen :smile:
ShellX "notepad.exe", vbNormalFocus

Rufst du die Function auf, so wird das Program oder die Batch gestartet. erst dann wird dein Program fortgesetzt. Also Rueckgabewert erhaelst du den ExitCode des Programmes :smile:
Möchtest du aber Dos Befehle ausführen, so musst du den Schalter /c benutzen.
Bsp.

 Dim sCmd As String
 Dim sShell As String
 sCmd = "dir > C:\Test.tmp"
 sShell = Environ$("COMSPEC")
 ShellX sShell & " /c " & sCmd, vbHide

Und wenn wir einmal dabei sind. Für alle VBA Freaks :wink:
Unter VBA sind die VB-Enumerationen nicht bekannt!
Da diese intern als Long betrachtet werden, kann man die Declaration auch so schreiben

Public Function ShellX(
 ByVal PathName As String,
 Optional ByVal WindowStyle As Long = vbMinimizedFocus,
 Optional ByVal Events As Boolean = True
 ) As Long

MfG Alex