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

Provost
Provost used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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 ...

Author

Commented:
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.

Author

Commented:
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
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
The caret ^ as shown in my code is required to "escape" the pipe character

Author

Commented:
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
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
That is because your command is wrong. findstr searches for every word you provide, not the whole sentence. The result you have seen is the last matching line.
for /F "delims=" %T in ('net localgroup "Utilisateurs du Bureau à distance" ^| findstr /c:"Utilisateurs du Bureau à distance"') do set var=%T

Open in new window

should work - this searches for the complete phrase.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial