Il existe un moyen avec la commande IF d'inclure une variable dans un ensemble de valeurs?Variable incluse dans un ensemble
Je veux dire:
IF %% i in (abc 123 OPL) echo premier set
IF %% i in (xyz 456 BNM) echo second ensemble
Il existe un moyen avec la commande IF d'inclure une variable dans un ensemble de valeurs?Variable incluse dans un ensemble
Je veux dire:
IF %% i in (abc 123 OPL) echo premier set
IF %% i in (xyz 456 BNM) echo second ensemble
de la ligne de commande:
C:\Users\preet>set val=99
C:\Users\preet>for %f in (100 99 21) do @if (%f)==(%val%) echo found it %f
found it 99
Dans un fichier batch
set val=99
for %%f in (100 99 21) do @if (%%f)==(%val%) echo found it %%f
Vous pouvez utiliser l'instruction for
pour ce faire. Voici un script qui vous permet de l'exécuter avec quelque chose comme:
myprog 456
et sortie sera in set 2
:
@setlocal enableextensions enabledelayedexpansion
@echo off
for %%a in (abc 123 opl) do (
if "x%%a"=="x%1" echo in set 1
)
for %%a in (xyz 456 bnm) do (
if "x%%a"=="x%1" echo in set 2
)
@endlocal
je l'ai vu fonctionne, mais je voudrais passer la variable à la boucle à partir du fichier de commandes, donc je voudrais mettre %% a = 123 au début du code. La commande que vous avez écrite est une "variable avec modificateur"? – aemme
Ensuite, vous venez de mettre quelque chose comme 'set xx = 7' au début du script et utilisez'% xx% 'au lieu de'% 1'. – paxdiablo
Merci beaucoup. Le paramètre que vous avez utilisé est un modificateur de la commande FOR, j'ai essayé de le chercher dans l'aide de Windows mais je ne l'ai pas trouvé. – aemme
Et vous n'êtes pas non plus limité à juste lot dans une machine Windows. Il y a aussi vbscript (et powershell). Voici comment vous pouvez vérifier en utilisant l'utilisation vbscript
strVar = WScript.Arguments(0)
Set dictfirst = CreateObject("Scripting.Dictionary")
Set dictsecond = CreateObject("Scripting.Dictionary")
dictfirst.Add "abc",1
dictfirst.Add "123",1
dictfirst.Add "opl",1
dictsecond.Add "xyz",1
dictsecond.Add "456",1
dictsecond.Add "bnm",1
If dictfirst.Exists(strVar) Then
WScript.Echo strVar & " exists in first set"
ElseIf dictsecond.Exists(strVar) Then
WScript.Echo strVar & " exists in second set"
Else
WScript.Echo strVar & " doesn't exists in either sets"
End If
:
C:\test>cscript //nologo test.vbs abc
abc exists in first set
C:\test>cscript //nologo test.vbs xyz
xyz exists in second set
C:\test>cscript //nologo test.vbs peter
peter doesn't exists in either sets
ok merci, c'est la façon simple que je préfère – aemme