OVH Cloud OVH Cloud

Commande Dos

2 réponses
Avatar
Jacques \(BE\)
Bonjour

Je lance la commande suivante :

Sub TifToGif()

ShellExecute Me.hwnd, "open", RepSel & "\TifToJpg.bat", vbNullString,
RepSel, SW_SHOWNORMAL

DoEvents

Kill RepSel & "\TifToJpg.bat"

End Sub

si je fais du pas à pas (F8) pas de problème: la fenêtre Dos s'ouvre le code
s'exécute et la fenêtre Dos se ferme.
Par contre si j'exécute le code normalement, la fenêtre Dos s'ouvre et se
referme aussi vite et n'exécute pas mon fichier .bat.

Comment remédier à cela ?

Merci de vos réponses

Jacques

2 réponses

Avatar
Jacques93
Bonsoir Jacques (BE),

Jacques (BE) a écrit :
Bonjour

Je lance la commande suivante :

Sub TifToGif()

ShellExecute Me.hwnd, "open", RepSel & "TifToJpg.bat", vbNullString,
RepSel, SW_SHOWNORMAL

DoEvents

Kill RepSel & "TifToJpg.bat"

End Sub

si je fais du pas à pas (F8) pas de problème: la fenêtre Dos s'ouvre le code
s'exécute et la fenêtre Dos se ferme.
Par contre si j'exécute le code normalement, la fenêtre Dos s'ouvre et se
referme aussi vite et n'exécute pas mon fichier .bat.




Ce qu'il faut, me semble t-il, c'est attendre la fin de l'exécution du
batch ou de la commande. Pour cela, tu peux utiliser les APIs :

'----------------------------------------------------------------------
Option Explicit

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
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 Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Const STARTF_RUNFULLSCREEN = &H20

Private Sub Command1_Click()
Dim retval As Long

retval = ExecCmd("toto.bat")
MsgBox "Le processus est terminé, code de sortie : " & retval
End Sub

Public Function ExecCmd(CmdLine As String) As Long
Dim proc As PROCESS_INFORMATION
Dim si As STARTUPINFO
Dim ret As Long

' Initialisez la structure STARTUPINFO :
si.cb = Len(si)
' Démarrez l'application Shell :
ret = CreateProcessA(vbNullString, CmdLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, si, proc)
' Attendre la fin de l'application Shell :
ret = WaitForSingleObject(proc.hProcess, INFINITE)
GetExitCodeProcess proc.hProcess, ret
CloseHandle proc.hThread
CloseHandle proc.hProcess
ExecCmd = ret
End Function

'---------------------------------------------------------------------
' ou utiliser Windows Script Host Object Model (Référence à ajouter au '
' projet : wshom.ocx)
'---------------------------------------------------------------------

Private Sub Command1_Click()
Dim retval As Long

retval = WshRun("toto.bat")
MsgBox "Le processus est terminé, code de sortie : " & retval
End Sub

Private Function WshRun(CmdLine As String) As Long
Dim wsh As WshShell
Dim ret As Long

Set wsh = New WshShell
ret = wsh.Run(CmdLine, 5, True)
WshRun = ret
Set wsh = Nothing
End Function


--
Cordialement,

Jacques.
Avatar
Ivan Pequeño
Merci aux deux Jacques

J'avais exactement le meme Pb
en faisant du Ftp de et vers un Host IBM 3270
je pense que celle-ci est la vrai solution

Merci encore