Lancer, Quitter un programme, une optimisation de ce code ?
Bonsoir.
Je débute tout juste mon apprentissage sur les scripts Batch et j'aimerais avoir quelques conseils afin d'optimiser un script qui lance/quitte un programme (exemple Skype) via un menu, et qui affiche si celui ci fonctionne ou pas.
Je me suis aidé de pas mal de guides et exemples sur le net et je vous prie de m'excuser pour les petites maladresses que je pourrais fournir dans le script à suivre.
Voici mes questions:
1. Est-ce que ce script peut être optimiser ? Utilisation de variables ? Meilleure syntaxe ou présence de script inutile ?
En effet, je souhaite continuer avec divers processus/programmes à fermer avant de pouvoir lancer des jeux vidéos (j'aimerais faire l'équivalent d'un GameBooster dont le plus connu est sans doute Razer Cortex)
2. Lorsque je clique sur une option, j'obtiens le texte "Launching Skype... Please wait few seconds." je ne suis pas certain du script correspondant, en effet j'aimerais avoir une attente de 5 secondes (5 0000 miliseconds?) et m'affiche ensuite "Skype is running".
3. J'aimerais vérifier qu'un programme/processus est en cours de fonctionnement et ainsi ignorer ou lancer le programme si celui ci n'est pas lancé.
4. Je n'arrive pas à mettre des couleurs spécifique sur un texte. is running = vert; is not running = rouge
Je vous remercie beaucoup de votre aide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
@echo off
cls
title Run and Close Skype
echo ==================================================================
echo [1] Run Skype
echo [2] Close Skype
echo [3] Skype Status (is it running?)
echo ==================================================================
:Option
SET /P INPUT="Choose an option:"
IF /I '%INPUT%'=='1' GOTO Run
IF /I '%INPUT%'=='2' GOTO Close
IF /I '%INPUT%'=='3' GOTO Status
IF /I '%INPUT%'=='Q' GOTO Quit
:Run
echo.
echo Launching Skype... Please wait few seconds.
start "" /b /min /LOW Skype.exe
ping 192.0.2.2 -n 1 -w 5000>nul 2>nul
tasklist /FI "IMAGENAME eq Skype.exe" 2>NUL | find /I /N "Skype.exe">NUL
if "%ERRORLEVEL%"=="0" echo Skype is running.
if "%ERRORLEVEL%"=="1" echo Skype is not running.
echo.
GOTO Option
:Close
echo.
echo Closing Skype... Please wait few seconds.
taskkill /f /im Skype.exe>NUL
ping 192.0.2.2 -n 1 -w 2000>nul 2>nul
tasklist /FI "IMAGENAME eq Skype.exe" 2>NUL | find /I /N "Skype.exe">NUL
::If Skype if closed, status is running... problem?
if "%ERRORLEVEL%"=="0" echo Skype is running.
if "%ERRORLEVEL%"=="1" echo Skype is not running.
echo.
GOTO Option
:Status
echo.
echo Checking Skype status... Please wait few seconds.
ping 192.0.2.2 -n 1 -w 1000>nul 2>nul
tasklist /FI "IMAGENAME eq Skype.exe" 2>NUL | find /I /N "Skype.exe">NUL
if "%ERRORLEVEL%"=="0" echo Skype is running.
if "%ERRORLEVEL%"=="1" echo Skype is is not running.
echo.
GOTO Option
:Quit
EXIT |
[Bat] AutoConnecting to Skype
Testez en premier lieu ce batch sans l'intégrer dans le menu, alors si ça marche chez vous ? vous pouvez ensuite essayer de l'intégrer au programme principal ;) :king:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @echo off
set SkypeUsername=UserName
Title AutoConnecting to Skype with this %SkypeUsername%
set SkypePassword=Password
set Process=Skype.exe
set SkypeLogin=/username:%SkypeUsername% /password:%SkypePassword% /nosplash
cls & color 0B
echo.
echo Checking if the %Process% is running or not...
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %Process%"') DO IF %%x == %Process% goto FOUND
start "" /b /min /LOW %Process% %SkypeLogin% >NUL
echo Connecting to Skype with %SkypeUsername%
ping 192.0.2.2 -n 1 -w 3000>nul 2>nul
goto FIN
:FOUND
cls & color 0A
echo The %Process% is running and connected to %SkypeUsername%
:FIN
echo Appuyez sur touche pour quitter ce batch
pause>Nul
Exit |