{"id":310,"date":"2013-03-11T13:27:08","date_gmt":"2013-03-11T12:27:08","guid":{"rendered":"http:\/\/sam-soul.fr\/Info\/?p=310"},"modified":"2013-03-11T13:27:08","modified_gmt":"2013-03-11T12:27:08","slug":"test-if-else","status":"publish","type":"post","link":"http:\/\/les-fougs.com\/wordpress\/2013\/03\/11\/test-if-else\/","title":{"rendered":"Test IF &#8230; ELSE"},"content":{"rendered":"<p>Test ERRORLEVEL avec les enableDelayedExpansion<\/p>\n<p>IF !ERRORLEVEL!==0 (<br \/>\necho condition vrai<br \/>\n<span style=\"color: #0000ff;\">) ELSE ( &#8212; facultative<br \/>\necho condition fausse &#8212; facultative<br \/>\n<\/span>)<br \/>\ndel \/F \/Q c:\\test_proc.tmp<br \/>\nTIMEOUT \/T 2 \/NOBREAK<br \/>\necho *******************************************<br \/>\n)<\/p>\n<p>Lance l&rsquo;ex\u00e9cution conditionnelle dans un programme de commandes.<\/p>\n<p>IF <span style=\"background-color: #ffff99;\">[NOT]<\/span> <span style=\"background-color: #ff99cc;\">ERRORLEVEL nombre<\/span> <span style=\"background-color: #00ff00;\">commande<\/span><br \/>\nIF <span style=\"background-color: #ffff99;\">[NOT]<\/span> <span style=\"background-color: #00ffff;\">cha\u00eene1==cha\u00eene2<\/span> <span style=\"background-color: #00ff00;\">commande<\/span><br \/>\nIF <span style=\"background-color: #ffff99;\">[NOT]<\/span> <span style=\"background-color: #ff9900;\">EXIST nom_de_fichier<\/span> <span style=\"background-color: #00ff00;\">commande<\/span><\/p>\n<p><strong><span style=\"background-color: #ffff99;\">NOT<\/span><\/strong> : Indique que Windows 2000 n&rsquo;effectue la commande que si la condition est fausse.<\/p>\n<p><strong><span style=\"background-color: #ff99cc;\">ERRORLEVEL nombre<\/span><\/strong> : Condition vraie si le dernier programme ex\u00e9cut\u00e9 a retourn\u00e9 un code sortie sup\u00e9rieur\/\u00e9gal au nombre donn\u00e9.<\/p>\n<p><strong><span style=\"background-color: #00ff00;\">commande<\/span> <\/strong>: Sp\u00e9cifie la commande \u00e0 ex\u00e9cuter si la condition est satisfaite.<\/p>\n<p><strong><span style=\"background-color: #00ffff;\">cha\u00eene1==cha\u00eene2<\/span><\/strong> : Condition vraie si les cha\u00eenes sp\u00e9cifi\u00e9es sont identiques.<\/p>\n<p><strong><span style=\"background-color: #ff9900;\">EXIST nom_de_fichier<\/span> <\/strong>: Condition vraie si le nom de fichier donn\u00e9 existe.<\/p>\n<p><strong><span style=\"background-color: #00ff00;\">\u00a0<\/span><\/strong><\/p>\n<p><strong><span style=\"background-color: #00ff00;\">commande :<\/span><\/strong> Sp\u00e9cifie la commande \u00e0 ex\u00e9cuter si la condition est v\u00e9rifi\u00e9e. <span style=\"background-color: #00ff00;\">Commande<\/span> peut \u00eatre suivi de la commande ELSE qui ex\u00e9cutera la commande situ\u00e9e apr\u00e8s le mot-cl\u00e9 ELSE si la condition est fausse (FALSE)<\/p>\n<p>La clause ELSE doit \u00eatre sur la m\u00eame ligne que la commande suivant IF.<br \/>\nPar exemple:<\/p>\n<p><span style=\"background-color: #ffff00;\">IF EXIST nomfichier. (<br \/>\ndel nomfichier.<br \/>\n) ELSE (<br \/>\necho nomfichier. introuvable.<br \/>\n)<\/span><\/p>\n<p><span style=\"color: #ff0000;\">L&rsquo;exemple suivant <strong>NE<\/strong> <strong>marche<\/strong> <strong>PAS<\/strong> car la commande del doit \u00eatre termin\u00e9e <\/span><span style=\"color: #ff0000;\">par une nouvelle ligne.<\/span><\/p>\n<p><span style=\"background-color: #ffff00;\">IF EXIST nomfichier. del nomfichier. ELSE echo nomfichier. introuvable<\/span><\/p>\n<p><span style=\"color: #ff0000;\">L&rsquo;exemple suivant <strong>NE marche PAS<\/strong> non plus, car la commande ELSE doit \u00eatre sur la m\u00eame ligne que la fin de la commande IF :<\/span><\/p>\n<p><span style=\"background-color: #ffff00;\">IF EXIST nomfichier. del nomfichier.<br \/>\nELSE echo nomfichier. introuvable<\/span><\/p>\n<p><span style=\"color: #008000;\">Enfin, l&rsquo;exemple suivant marche si vous l&rsquo;\u00e9crivez sur une seule ligne :<\/span><\/p>\n<p><span style=\"background-color: #ffff00;\">IF EXIST nomfichier. (del nomfichier.) ELSE echo nomfichier. introuvable<\/span><\/p>\n<p>Si les extensions de commandes sont activ\u00e9es, IF est modifi\u00e9 comme suit :<br \/>\nIF <strong><span style=\"background-color: #ffcc99;\">[\/I]<\/span><\/strong> Cha\u00eene1 <strong><span style=\"background-color: #00ff00;\">op-de-comp<\/span><\/strong> Cha\u00eene2 commande<br \/>\nIF <span style=\"background-color: #00ffff;\">CMDEXTVERSION<\/span> nombre commande<br \/>\nIF <span style=\"background-color: #ffcc00;\">DEFINED<\/span> variable commande<\/p>\n<p>o\u00f9 <strong><span style=\"background-color: #00ff00;\">op-de-comp<\/span><\/strong> peut \u00eatre :<\/p>\n<p>EQU &#8211; \u00e9gal \u00e0<br \/>\nNEQ &#8211; diff\u00e9rent de<br \/>\nLSS &#8211; inf\u00e9rieur \u00e0<br \/>\nLEQ &#8211; inf\u00e9rieur ou \u00e9gal \u00e0<br \/>\nGTR &#8211; sup\u00e9rieur \u00e0<br \/>\nGEQ &#8211; sup\u00e9rieur ou \u00e9gal \u00e0<\/p>\n<p>et le commutateur <strong><span style=\"background-color: #ffcc99;\">\/I<\/span><\/strong>, s&rsquo;il est sp\u00e9cifi\u00e9, demande d&rsquo;ignorer la casse dans la comparaison des cha\u00eenes. Le commutateur \/I peut \u00e9galement \u00eatre utilis\u00e9 pour la forme Cha\u00eene1==Cha\u00eene2 de IF. Ces comparaisons sont g\u00e9n\u00e9riques : si les cha\u00eenes Cha\u00eene1 et Cha\u00eene2 sont toutes deux faites uniquement de chiffres, alors elles sont converties en nombres, et une comparaison num\u00e9rique est alors effectu\u00e9e.<\/p>\n<p>La condition <span style=\"background-color: #00ffff;\">CMDEXTVERSION<\/span> marche comme ERRORLEVEL, sauf qu&rsquo;elle compare \u00e0 un num\u00e9ro de version interne associ\u00e9 aux extensions de commandes. La premi\u00e8re version des extensions est 1. Ce num\u00e9ro sera incr\u00e9ment\u00e9 de un \u00e0 chaque am\u00e9lioration significative. La condition CMDEXTVERSION n&rsquo;est jamais vraie lorsque les extensions de commandes sont d\u00e9sactiv\u00e9es.<\/p>\n<p>La condition <span style=\"background-color: #ffcc00;\">DEFINED<\/span> fonctionne comme EXISTS, sauf qu&rsquo;elle prend un nom de variable d&rsquo;environnement et renvoie vrai si la variable d&rsquo;environnement est d\u00e9finie.<\/p>\n<p>%ERRORLEVEL% sera une repr\u00e9sentation sous forme de cha\u00eene de la valeur en cours de ERRORLEVEL, pourvu qu&rsquo;il n&rsquo;existe pas d\u00e9j\u00e0 de variable d&rsquo;environnement portant le nom ERRORLEVEL, auquel cas ce serait la valeur de cette derni\u00e8re que vous obtiendriez. En utilisant cela et les comparateurs num\u00e9riques ci-dessous, vous pouvez faire :<\/p>\n<p>goto answer%ERRORLEVEL%<\/p>\n<p>:answer0<\/p>\n<p>echo Le programme a retourn\u00e9 le code 0<\/p>\n<p>:answer1<\/p>\n<p>echo Le programme a retourn\u00e9 le code 1<\/p>\n<p>vous pouvez aussi faire, en utilisant les comparaisons num\u00e9riques ci-dessus :<\/p>\n<p>IF %ERRORLEVEL% LEQ 1 goto okay<\/p>\n<p>%CMDCMDLINE% sera la ligne de commande originale pass\u00e9e \u00e0 CMD.EXE avant tout traitement par CMD.EXE, pourvu qu&rsquo;il n&rsquo;existe pas d\u00e9j\u00e0 de variable d&rsquo;environnement portant le nom CMDCMDLINE, auquel cas ce serait la valeur de cette derni\u00e8re que vous obtiendriez.<\/p>\n<p>%<span style=\"background-color: #00ffff;\">CMDEXTVERSION<\/span>% sera une cha\u00eene repr\u00e9sentant la valeur actuelle de<\/p>\n<p><span style=\"background-color: #00ffff;\">CMDEXTVERSION<\/span>, tant qu&rsquo;il n&rsquo;existe pas d\u00e9j\u00e0 de variable d&rsquo;environnement appel\u00e9e CMDEXTVERSION, dans quel cas vous obtiendrez sa valeur \u00e0 la place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Test ERRORLEVEL avec les enableDelayedExpansion IF !ERRORLEVEL!==0 ( echo condition vrai ) ELSE ( &#8212; facultative echo condition fausse &#8212; facultative ) del \/F \/Q c:\\test_proc.tmp TIMEOUT \/T 2 \/NOBREAK echo ******************************************* ) Lance l&rsquo;ex\u00e9cution conditionnelle dans un programme de commandes. IF [NOT] ERRORLEVEL nombre commande IF [NOT] cha\u00eene1==cha\u00eene2 commande IF [NOT] EXIST nom_de_fichier commande &hellip; <a href=\"http:\/\/les-fougs.com\/wordpress\/2013\/03\/11\/test-if-else\/\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Test IF &#8230; ELSE&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[],"_links":{"self":[{"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/posts\/310"}],"collection":[{"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":0,"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"wp:attachment":[{"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/les-fougs.com\/wordpress\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}