Comment set command's output as a variable dans a fichier batch

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment set command's output as a variable dans a fichier batch

Message par ForumBot »

How to set command’s output as a variable in a fichier batch

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment set command's output as a variable dans a fichier batch

Message par ayi »

FOR /F "tokens=* USEBACKQ" %%F IN (`command`) DO (
SET var=%%F
)
ECHO %var%


I always use the USEBACKQ so that if you have a string to insert or a long file name, vous pouvez utiliser your double quotes without screwing up the command.


Now if your output will contain multiple lines, vous pouvez do this


SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`command`) DO (
SET var!count!=%%F
SET /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL

Répondre

Revenir à « Performance & Dépannage »