Link to home
Start Free TrialLog in
Avatar of Provost
Provost

asked on

Put the result of findstr in a variable in a batch file on Windows

Could you tell me how to put the result of this command in a variable in a batch script?

If i type this line: echo "This is a test" | findstr "is"

I got this: "This is a test"

I want this result i a variable
Avatar of Qlemo
Qlemo
Flag of Germany image

In a batch file:
for /F "delims=" %%T in ('echo "This is a test" ^| findstr "is"') do set var=%%T

Open in new window

but that is probably too simplified for being used for an actual purpose ...
Avatar of Provost
Provost

ASKER

If i try this in a command line (this is the real thing)

for /F "delims=" %T in ('net localgroup "Utilisateurs du Bureau à distance" | findstr "ADMINISTRATIF\SAV$cont-exec"') do set var=%T

I got

| was expected.
Avatar of Provost

ASKER

This comman alone return what i'm looking for.

The goal is to check if a user is member of a localgroup.

net localgroup "Utilisateurs du Bureau à distance" | findstr "ADMINISTRATIF\SAV$cont-exec"
return this:
ADMINISTRATIF\SAV$cont-exec
The caret ^ as shown in my code is required to "escape" the pipe character
Avatar of Provost

ASKER

for /F "delims=" %T in ('net localgroup "Utilisateurs du Bureau à distance" ^| findstr "Utilisateurs du Bureau à distance"') do set var=%T

echo %var%
return this instead of ADMINISTRATIF\SAV$cont-exec

Commentaire     Les membres de ce groupe disposent des droits nécessaires pour ouvrir une session à distance
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial