iss42
iss42

Reputation: 2816

Windows batch: for and findstr to include and exclude from results

I have a list of strings I'm iterating over, and I need to exclude certain ones and include others in the subsequent processing. Something like this:

listmystuff | for /F "usebackq" %%D in (`findstr /r "something*" ^| findstr /v "but not thisstuff"`)  do interestingthing

This is wrong. What does work is having one of the findstr's but not both. What would be right?

Upvotes: 1

Views: 3387

Answers (2)

foxidrive
foxidrive

Reputation: 41234

Give this a run with your show databases command. This is the usual way to use a for command to parse data.

for /F "delims=" %%D in ('show databases ^| findstr /r "something*" ^| findstr /v /c:"but not thisstuff" ')  do echo %%D

Upvotes: 0

dbenham
dbenham

Reputation: 130839

You want a pipe |, not conditional command concatenation &&. The pipe character must be escaped.

You only want one set of single quotes around the entire commmand. (or backticks if using usebackq option)

You need to double the percents if used within a batch file.

Your initial FINDSTR needs a filespec to search (or else data piped into it)

for /f "options" %%D in ('findstr "something" fileSpec ^| findstr /v "butNotThis"') do myThing

Upvotes: 2

Related Questions