TUTOS.EU

Lancer une application en VbScript en la passant en paramètre et attendre queques secondes

Lancer un programme en VbScript et attendre un temps déterminé en secondes

Option Explicit
'Launch an application and wait some seconds

Dim WSHShell, MonExe, PIDMonExe
Dim DateLancement
Dim CheminExeALancer, MaxDuration

Function ParseCommand()
	'
	' Parses the command line and fills the script variables 
	' with the appropriate values.
	'
	Dim ArgCount
	Dim objArgs

	Set objArgs = Wscript.Arguments

	ArgCount = 0
	if objArgs.Count = 0 then
		wscript.echo "No arguments specified."
		wscript.echo
		call Help()
	end if

	While ArgCount < objArgs.Count
		Select Case LCase(objArgs(ArgCount))
			Case "-app"
			ArgCount = ArgCount + 1
			CheminExeALancer=LCase(objArgs(ArgCount))
			wscript.echo "CheminExeALancer : " & CheminExeALancer

			Case "-duration"
			ArgCount = ArgCount + 1
			MaxDuration=LCase(objArgs(ArgCount))
			If IsNumeric(MaxDuration) = True Then
				MaxDuration = CInt(MaxDuration)
				wscript.echo "MaxDuration : " & MaxDuration
			Else
				wscript.echo "-duration : n'est pas un numérique " & MaxDuration
				Wscript.Quit
			End If


			Case Else:
			wscript.echo "Invalid command."
			wscript.echo
			call Help()
			wscript.quit
		End Select
		ArgCount = ArgCount + 1
	Wend
End Function

sub Help()
	'
	' Display command-line syntax for the script.
	'
	wscript.echo "Script Function details"
	wscript.echo "Syntax:"
	wscript.echo
	wscript.echo "-app ""patch to the source exe"""
	wscript.echo "-duration ""time to wait in seconds"""
	wscript.echo
	wscript.echo "Example"
	wscript.echo
	wscript.echo "VbLaunchAppAndWaitSomeSeconds.vbs -app ""Notepad.exe C:\un fichier.txt"" -duration 5"
	wscript.quit
End Sub

'Parse the command line.
call ParseCommand()


Set WSHShell = CreateObject("WScript.Shell")
Set MonExe = WSHShell.Exec(CheminExeALancer) 'Lancement de Notepad
'PIDMonExe = MonExe.ProcessID
Set MonExe = Nothing
Set WSHShell = Nothing

DateLancement = Now

MaxDuration = MaxDuration * 1000
WScript.Sleep MaxDuration 'Attente de x secondes
'Wscript.echo "Exe terminé"
Lien vers le fichier : cliquez ici Copier le code

2